Types and Interfaces

Interface is just another type of the CLR. Interfaces can have members, with the restriction that an interface cannot have instance fields nor instance methods with implementation.

One can view interfaces as partitioning the set of all possible objects into subsets. Which subsets an object belongs to depends on which interfaces the object’s type has declared compatibility with.

The CLR allows the concrete type to declare the methods interface forced as private provided that one uses some mechanism to indicate that the methods are used to satify the requirements of the interface, which hides the method from the concrete type’s public contract. To access the hided methods, one must use a reference of the interface type, which can acces all interface type declared methods. Consider the following example:

namespace EssentialNet

{

    interface ICustomer

    {

        string GetBirthDay();

    }

}

namespace EssentialNet

{

    public class Customer : ICustomer

    {

        public static string name;

 

        public static string id = "C1";

 

        public static string code = "XIECH2";

 

        public long t1 = DateTime.Now.Ticks;

 

        public long t2 = DateTime.Now.Ticks;

 

        public long t3;

 

         static Customer()

        {

            name = "xiech2";

        }

 

        public Customer()

        {

            t3 = DateTime.Now.Ticks;

        }

 

        #region ICustomer Members

 

        string ICustomer.GetBirthDay()

        {

            return "20100101";

        }

 

        #endregion

    }

}

To access the GetBirthDay method, on must use a reference of type Icustomer.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值