码迷,mamicode.com
首页 > 其他好文 > 详细

Property

时间:2014-07-21 10:03:52      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   os   

Interface Property

  Properties can be declared on an interface (C# Reference)

  bubuko.com,布布扣

  按如下形式实现interfac来避免冲突。

  bubuko.com,布布扣

  在没有interface前缀的情况下,编译器不会报错,2个interface引用同一方法:  

bubuko.com,布布扣
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
View Code

  为每个interface实现不同的方法可以按如下这样:

bubuko.com,布布扣
public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}
View Code

 bubuko.com,布布扣

  访问器可以为virtual,这也意味着可以override:

bubuko.com,布布扣
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; }
    }
}
View Code

 

参考:

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

Property,布布扣,bubuko.com

Property

标签:style   blog   http   color   strong   os   

原文地址:http://www.cnblogs.com/tekkaman/p/3856054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!