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("交作业了!");
        }
    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(面向对象-多态_接口))