Interface

结构定义了一个共同 的约定,在继承和使用的时候可以使用不同的方案实现相同的目的。而且可以实现多重的继承。在实现接口的时候其访问类型必须为public。

class Program
    {
        static void Main(string[] args)
        {
            Person P1 = new Person();
            P1.name = "Wang";
            Person P2 = new Person();
            Console.WriteLine("The result of the P1 compare to P2 {0}", P1.CompareToTst(P2));

            IComparableTst If_P1 = new Person();
            
            IComparableTst If_P2 = new Person();
            Console.WriteLine("The result of the If_P1 compare to If_P2 {0}", If_P1.CompareToTst(If_P2));
            Console.ReadKey();
        }
    }

    class Person: IComparableTst
    {
        public string name;
        public bool sex;
        public int age;
        public Person()
        {
            name = "Lee";
            sex = false;
            age = 18;
        }
        public int CompareToTst(object obj)
        {
            Person p = (Person)obj;
            int st = 0;
            if (this.age == p.age) st = 0;
            if (this.age < p.age) st = -1;
            if (this.age > p.age) st = 1;
            return st;
        }
    }

    interface IComparableTst
    {
        int CompareToTst(object obj);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容