接口

  ---->接口是一种能力
  ---->接口也是一种规范
  ---->如果你继承了这个接口,就必须按照接口的要求来实现这个接口。(如Usb口)
  interface I开头...able结尾

  只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员

  接口的功能要单一

  为了多态。 接口不能被实例化。
  也就是说,接口不能new(不能创建对象)
  跟抽象类是一样的,因为创建对象也没意义。


  接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。
  类中的成员默认的访问修饰符是private ,而接口中默认的访问修饰符是public

  (默认为public) 接口中的成员不能有任何实现(“光说不做”,只是定义了一组未实现的成员)。


  接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。

  接口与接口之间可以继承

  接口并不能去继承一个类,而类可以继承接口 (接口只能继承于接口,而类既可以继承接口,也可以继承类)

  实现接口的子类必须实现该接口的全部成员。

  一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。


  class MyClass:A,IA{},因为类是单继承的。

 

  显示实现接口的目的:解决方法的重名问题
  什么时候显示的去实现接口:
    当继承的接口中的方法和参数一摸一样的时候,要是用显示的实现接口

    当一个抽象类实现接口的时候,需要子类去实现接口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
    class Program
    {
        static void Main(string[] args)
        {
            //接口不能被实例化
            //IDraw d1 = new IDraw();

            //但是可以将接口指向继承该接口的子类
            IDraw d1 = new Student();
            d1.Draw();//显示Student likes draw
            //d1.Speak();//接口没有包含这个定义
             
            IDraw d2 = new Teacher();
            d2.Draw();//显示Teacher likes draw

            Student stu = new Student();
            stu.Draw();
            stu.Speak();

            

            Console.WriteLine("\n\n");
            MyPerson per1 = new MyPersonSon();
            per1.Eat();//虚方法的override,调用MyPersonSon中实现
            per1.Drink();//实方法中new,调用MyPerson中实现
            MyPerson per2 = new MyPersonDaughter();
            per2.Eat();//虚方法的override,调用MyPersonDaughter中实现
            per2.Drink();//实方法中new,调用MyPerson中实现
            //Console.ReadKey();

            Console.WriteLine("\n\n");
            MyPersonSon per3 = new MyPersonSon();
            per3.Eat();//虚方法的override,调用MyPersonSon中实现
            per3.Drink();//实方法中new,调用MyPersonSon中实现
            Console.ReadKey();
        }
    } 

    //子类至多只继承一个父类,但可以继承多个接口
     class Person
     {
         public void Speak()
         {
             Console.WriteLine("Sleep");
         }
     }

    interface IDraw//可以继承其他接口,但是不能继承类
    {
        //接口成员是public,但是不能加public
        void Draw();
        //接口的功能要单一,不能有其他行为
        //void Eat();

        //
        //接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数(不能被实例化)
    }

    //继承父类,对父类成员可以重写,使用override
    //继承接口,对接口成员需要实现
    //继承接口的子类,需要实现所有接口的成员,如果一个子类同时继承
    //了父类A和接口IA,那么父类必须放在接口之前,因为类是单继承的
    //class MyClass : A, IA { }
    class Student:Person,IDraw//基类必须在接口之前
    {
        public void Draw()
        {
            Console.WriteLine("Student likes draw");
        }

        public new void Speak()
        {
            Console.WriteLine("Student Sleep");
        }
    }

    class Teacher:Person,IDraw
    {

        public void Draw()
        {
            Console.WriteLine("Teacher likes draw");
        }

        public new void Speak()
        {
            Console.WriteLine("Teacher Sleep");
        }
    }

    interface IEat
    {
        void Eat();
    }
    
    interface IDrink
    {
        void Drink();
    }


    public abstract class MyPerson: IEat,IDrink
    {
        public void Test()
        {
            Console.WriteLine("抽象类实现自己的成员");
        }

        virtual public void Eat()
        {
            Console.WriteLine("抽象类实现接口成员Eat");
        }

        public void Drink()
        {
            Console.WriteLine("抽象类实现接口成员Drink");
        }
    }

    //尽量不要让父类继承接口
    //如果想实现多态,需将接口定义成虚函数,并在子类中override
    //如果不用虚函数,而是用普通函数,则在子类中new之后,使用父类变量引用子类对象,并不能实现多态。
    public class MyPersonSon : MyPerson
    {

        //public void Eat()
        //{
        //    throw new NotImplementedException();
        //} 
     
        //鼠标放在接口名称上,出现小蓝色框时,按下alt+shift+F10,选择显示实现接口
        override public void Eat()
        {
            Console.WriteLine("son 普通实现Eat");
        }

        new public void Drink()
        {
            Console.WriteLine("son 普通实现Drink");
        }
    }
     
    public class MyPersonDaughter : MyPerson
    {

        override public void Eat()
        {
            Console.WriteLine("daughter 普通实现Eat");
        }

        //隐藏了继承的MyPerson.Drink(),最好使用new,来显示是故意隐藏
        public void Drink()
        {
            Console.WriteLine("daughter 普通实现Drink");
        }
    }
}

 

练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口练习
{
    class Program
    {
        static void Main(string[] args)
        {
            DenJi(new Person());
            DenJi(new House());

            Console.ReadKey();
        }

        static void  DenJi(IDengJi dj)
        {
            dj.DengJi();
        }
    }

    interface IDengJi
    {
        void DengJi();
    }

    class Person:IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("人需要登记");

        }
    }

    class House : IDengJi
    {
        public void DengJi()
        {
            Console.WriteLine("房子需要登记");

        }
    }

}

  

 

posted @ 2017-09-30 14:00  mCat  Views(231)  Comments(0Edit  收藏  举报