C# 学习笔记(Interface) - 08

Interfaces enable to define behavior characteristics or abilities and apply them to classes, irrespective of the class hierarchy.

Give you the ability to define a set of semantically related method and properties that selected class can implement, regardless of the class hierarchy

Declaring Interfaces

Any class that implement an interface must define each and every memebers of that interface.

All of the interfaces methods are public by definition.

Implementing Interface

Cast an object to one of its implemented interfaces and then call one of these interface member.

IValidation val = (IValidation) ssn;
val.Validate();

Query for Implementation by Using is

expression is type

Query for Implementation by Using as

object = expression as type

Interfaces vs. the Alternatives

Explicit Interface Member Name Qualification

Prevent the Implemented memeber of interfaces from becoming public member of class.

Remove the member's access modifier "public" and qualify the member name with the interface name.

When you wan't to hide a member, you can't use an access modifier.

Avoiding Name Ambiguity

Removing the accessor modifier and prepending the member name with the interface name.

note:

  1. If specify that both interfaces are being implemeted but define only one of methods, the specification will result error;
  2. the client code need to cast to appropriate interface

Interface and inheritance

When you cast an object to an interface, the compiler will traverses the inheritance tree until a class is found that contains the interface in its base list.

Combining Interfaces

Public interface Icombo : IDragDrop, Iserializable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值