• 黑马程序员--多态练习(手机工厂)


    ---------------------- ASP.Net+Android+IOS开发 .Net培训期待与您交流! ----------------------

     interface IAppable
        {
            void applicatiable();
        }
    interface Icall
        {
           void call();
        }
      abstract class Phone:Icall
        {
             public abstract void call();     
        }
     abstract class Nokia:Phone
        {
            public string Mark = "诺基亚";
            public abstract override void call();
        }
     class Nokia型号8987:Nokia
        {
            public override void call()
            {
                Console.Write("我是功能机,我可以打电话,我的价格很便宜哟几百块钱哟.");
            }
        }
     class Nokia型号appable899:Nokia,IAppable
        {
    
            public override void call()
            {
                Console.Write("我可以打电话.我价格几千不等.");
            }
    
            public void applicatiable()
            {
                Console.WriteLine("我可以装软件,我很贵,我是智能机.");
            }
        }
    class Program
        {
            static void Main(string[] args)
            {
                //Nokia手机公司,批量生产Nokia
                Nokia[] ns ={
                               new Nokia型号8987(),
                               new Nokia型号appable899()
                           };
                for (int i = 0; i < ns.Length;i++ )
                {
                    ns[i].call();
                    Console.WriteLine("品牌是"+ns[i].Mark+"
    ");
                }
                //一个生产智能机的大公司
                IAppable[] IAphones ={
                                      new Nokia型号appable899(),
                                      new Nokia型号appable899()
                                    };
                for (int i = 0; i < IAphones.Length;i++ )
                {
                    IAphones[i].applicatiable();
                }
                Console.ReadKey();
            }
        }

     ■接口的实现类似于继承

      ♢补充方法体

    ■接口是"多继承"

      ♢由于多继承,无法保证重名的问题

      ♢可以显式实现接口成员

      ♢<接口名>.方法名(){/*方法体*/}

      ♢显式实现接口成员必须由接口对象来调用

    interface I1
        {
            void Func();
        }
        interface I2
        {
            void Func();
        }
        class Myclass:I1,I2
        {
            void I1.Func()//显式实现接口成员
            {
                Console.WriteLine("我是I1");
            }
    
            void I2.Func()//显式实现接口成员
            {
                Console.WriteLine("我是I2");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {//显式实现的接口的方法,只能使用接口对象来调用
                Myclass c = new Myclass();//访问不到成员Func(),无论是I1或I2的Func()
                ((I1)c).Func();
                ((I2)c).Func();
                Console.ReadKey();
            }
        }

     ■自动实现属性和接口属性

    interface IProperity
        {
            //接口的属性,只写一个"get;";或"set;";或都写;表示指定的属性类型
            int num1
            {
                set;
                get;
            }
            int num2
            {
                set;
            }
            int num3
            {
                get;
            }
        }
            class Myclass1: IProperity
            {
                public int num1
                {
                    get;  
                    set;
                }
                private int num2;
                public int Num2
                {
                    set { throw new NotImplementedException(); }
                }
                private int num3;
                public int Num3
                {
                    get { throw new NotImplementedException(); }
                }
            }
            class Myclass2
            {
                //自动实现属性
                int num//无后备字段
                {
                    set;//不实现方法体
                    get;//不实现方法体
                }
                //自动实现属性,必须同时具备set;和get;
            }
            class Program
            {
                static void Main(string[] args)
                {
                }
            }
  • 相关阅读:
    Servlet基本概念及其部署
    MSSQL数据库全库批量替换
    我的第一个GAE(google appengine)应用
    今天你有病了吗?
    [Microsoft][ODBC SQL Server Driver][DBNETLIB] 一般性网络错误
    Google appengine 上传输错用户名解决办法;
    查看畸形文件
    Session
    jq幻灯片2
    JS打开层/关闭层/移动层动画效果
  • 原文地址:https://www.cnblogs.com/tobecabbage/p/3468115.html
Copyright © 2020-2023  润新知