unity游戏开发-C#语言基础篇(面向对象-多态_接口)

 class Program
    {
        static void Main(string[] args)
        {
            Teacher teacher = new Teacher();
            Doshouzuoye(teacher);
            Student stu = new Student();
            Doshouzuoye(stu);//参数看实例化的类型

            

            Console.ReadKey();
        }

        private static void Doshouzuoye(IShouzuoye person)
        {

           person.shouzuoye();//调用子类的方法 看实例化的参数类型 student 或 teacher
        }

        
    }
 interface IShouzuoye
    {
         void shouzuoye();//接口 抽象前面不用加修饰符,默认public 加了也无效
    }
   class Student:IShouzuoye
    {
        public void shouzuoye()
        {
            //  throw new NotImplementedException();
            Console.WriteLine("报告老师,收集完毕!");
        }
    }
   class Teacher:IShouzuoye
    {
        public void shouzuoye()
        {
            //throw new NotImplementedException();
            Console.WriteLine("交作业了!");
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容