Unity3D開發-C#語言進階篇(面向對象之多態-接口應用詳解)

 class Program
    {
        static void Main(string[] args)
        {
            //1、創建一個名稱為Vehicle的接口,在接口中添加兩個帶有一個參數的方法start()和stop()。
            //    //在兩個名稱分別為Bike和Bus的類中實現Vehicle接口。
            //    //創建一個名稱為interfaceDemo的類,在interfaceDemo的main()方法中創建Bike和Bus對象,并訪問start()和stop()方法。
            //Bike bike = new Bike();
            //Bus bus = new Bus();

            //2、設計一張抽象的門Door類,那么對于這張門來說,就應該擁有所有門的共性,開門openDoor()和關門closeDoor();
            //然后對門進行另外的功能設計,防盜--theftproof()、防水--waterproof()、防彈--bulletproof()、防火、防銹…… 
            //要求:利用繼承、抽象類、接口的知識設計該門
            IDoors door = new NewDoors();
            Console.ReadKey();
        }
    }
 class Bike:IVechicle
    {
        public Bike()
        {

            this.start();
            this.stop();
        }



        public void start()
        {
            //throw new NotImplementedException();
            Console.WriteLine("Bike 開動了!");
        }

        public void stop()
        {
            // throw new NotImplementedException();
            Console.WriteLine("Bike 停止了!");
        }
    }
 class Bus:IVechicle
    {
        public Bus()
        {
            this.start();
            this.stop();

        }
        public void start()
        {
            // throw new NotImplementedException();
            Console.WriteLine("Bus 開動了!");
        }

        public void stop()
        {
            // throw new NotImplementedException();
            Console.WriteLine("Bus 停止了!");
        }
    }

 abstract class Doors
    {
        public abstract string Theftproof
        {
            get;
            set;
        }
        public abstract string Waterproof
        {
            get;
            set;
        }
        public abstract string Bulletproof
        {
            get;
            set;
        }
        public abstract string Fireproof
        {
            get;
            set;
        }
        public abstract string Xiuproof
        {
            get;
            set;
        }

    }
 interface IDoors
    {
        void openDoor();
        void closeDoor();
    }
 interface IVechicle
    {
        void start();

        void stop();
    }
  class NewDoors:Doors,IDoors
    {
        private string theftproof;
        private string waterproof;
        private string bulletproof;
        private string fireproof;
        private string xiuproof;


        public NewDoors()
        {
            this.openDoor();
            this.closeDoor();

            Console.WriteLine("門的屬性有:{0} {1} {2} {3} {4}", Theftproof, Waterproof, Bulletproof, Fireproof, Xiuproof);


        }



        public override string Theftproof
        {

            get
            {
                //throw new NotImplementedException();
                return "防盜";
            }
            set
            {

                theftproof = value;


            }

        }

        public override string Waterproof
        {
            get
            {
                //throw new NotImplementedException();
                return "防水";
            }
            set
            {
                // throw new NotImplementedException();

                waterproof = value;


            }
        }

        public override string Bulletproof
        {
            get
            {
                //throw new NotImplementedException();
                return "防彈";
            }
            set
            {
                // throw new NotImplementedException();

                bulletproof = value;


            }
        }

        public override string Fireproof
        {
            get
            {
                //throw new NotImplementedException();
                return "防火";
            }
            set
            {
                // throw new NotImplementedException();
                fireproof = value;


            }
        }

        public override string Xiuproof
        {
            get
            {
                // throw new NotImplementedException();
                return "防銹";
            }
            set
            {
                // throw new NotImplementedException();

                xiuproof = value;

            }
        }

        public void openDoor()
        {
            // throw new NotImplementedException();
            Console.WriteLine("門 打開了!");
        }

        public void closeDoor()
        {
            //throw new NotImplementedException();
            Console.WriteLine("門  被關上了!");
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容