接口 Interface

1.interface 关键字 声明接口;

2.接口中的成员默认都是public 也不能加“访问修饰符”,只要一个类继承了这个接口,就必须实现这个接口中的所有成员;

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

4.接口和接口之间可以继承,并且可以多继承;接口不能继承类,类可以继承接口

5.当虚方法继承接口时,需子类去实现;

6.能力不一样的时候适合用接口;

using System;
using System.Collections;
using System.Collections.Generic;
namespace Dome
{
    class dom
    {
        static void Main(string[] args)
        {
            play iplay = new student();
            iplay.iplay();
            Console.WriteLine();
            Console.ReadKey();
        }
    }
    public class person {//父类
        public void sayhello() { Console.WriteLine("我是人类"); }
    }
    public class student:person,play {//继承父类person和接口play  必须实现接口中的成员
        public void hello() { Console.WriteLine("我是学生"); }
        public void iplay() { Console.WriteLine("实现接口"); }
    }
    interface play {//接口中的成员默认是public
        void iplay();
    }
}
 
View Code

 显示实现接口

using System;
using System.Collections;
using System.Collections.Generic;
namespace Dome
{
    class dom
    {
        static void Main(string[] args)
        {
            play iplay = new person();
            iplay.iplay();
            person p = new person();
            p.iplay();
            Console.ReadKey();
        }
    }
    public class person:play {

        void play.iplay()//显示实现接口
        {
            Console.WriteLine("我是接口中的方法");
        }
        public void iplay() //类中的方法
        { Console.WriteLine("我是类中的方法"); }
    }
   
    interface play {//接口中的成员默认是public
        void iplay();
    }
}
 
View Code

 

posted @ 2017-04-02 21:42  ADFL  阅读(149)  评论(0编辑  收藏  举报