首页 > 代码库 > Property
Property
【Interface Property】
Properties can be declared on an interface (C# Reference).
按如下形式实现interfac来避免冲突。
在没有interface前缀的情况下,编译器不会报错,2个interface引用同一方法:
class Test { static void Main() { SampleClass sc = new SampleClass(); IControl ctrl = (IControl)sc; ISurface srfc = (ISurface)sc; // The following lines all call the same method. sc.Paint(); ctrl.Paint(); srfc.Paint(); }}interface IControl{ void Paint();}interface ISurface{ void Paint();}class SampleClass : IControl, ISurface{ // Both ISurface.Paint and IControl.Paint call this method. public void Paint() { Console.WriteLine("Paint method in SampleClass"); }}// Output:// Paint method in SampleClass// Paint method in SampleClass// Paint method in SampleClass
为每个interface实现不同的方法可以按如下这样:
public class SampleClass : IControl, ISurface{ void IControl.Paint() { System.Console.WriteLine("IControl.Paint"); } void ISurface.Paint() { System.Console.WriteLine("ISurface.Paint"); }}
访问器可以为virtual,这也意味着可以override:
public class Parent{ public virtual int TestProperty { // Notice the accessor accessibility level. protected set { } // No access modifier is used here. get { return 0; } }}public class Kid : Parent{ public override int TestProperty { // Use the same accessibility level as in the overridden accessor. protected set { } // Cannot use access modifier here. get { return 0; } }}
参考:
1、http://msdn.microsoft.com/zh-cn/library/64syzecx.aspx
2、http://msdn.microsoft.com/zh-cn/library/ms173157.aspx
3、http://msdn.microsoft.com/zh-cn/library/75e8y5dd.aspx
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。