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);

你可能感兴趣的:(Interface)