黑马程序员--多态练习(手机工厂)
2013-12-11 13:05
281 查看
---------------------
ASP.Net+Android+IOS开发.Net培训期待与您交流! ----------------------
■接口的实现类似于继承
♢补充方法体
■接口是"多继承"
♢由于多继承,无法保证重名的问题
♢可以显式实现接口成员
♢<接口名>.方法名(){/*方法体*/}
♢显式实现接口成员必须由接口对象来调用
■自动实现属性和接口属性
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+"\n"); } //一个生产智能机的大公司 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 { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public int num2 { set { throw new NotImplementedException(); } } public int num3 { get { throw new NotImplementedException(); } } } class Myclass2 { //自动实现属性 int num//无后备字段 { set;//不实现方法体 get;//不实现方法体 } //自动实现属性,必须同时具备set;和get; } class Program { static void Main(string[] args) { } }
相关文章推荐
- 黑马程序员--多态练习(手机工厂)
- 黑马程序员——Java练习笔记——多态
- 黑马程序员—java基础_异常多态练习
- 黑马程序员--OC自学笔记---07@property使用之电商app练习
- 黑马程序员——java基础之多态
- 练习ViewSwitcher例子出现的手机闪退现象
- 黑马程序员_java多态机制
- 《黑马程序员》 TreeMap练习分析
- 黑马程序员—面向对象特征之一多态
- 黑马程序员_继承与多态
- 黑马程序员_继承与多态
- 黑马程序员java学习第八天,多态
- 【黑马程序员】java中--------------封装、继承、多态
- 黑马程序员 正则表达式的练习
- 黑马程序员_多态的理解
- 黑马程序员----FileReader和FileWriter练习-简单的文件copy
- 黑马程序员-继承和多态
- 黑马程序员——黑马学习日志之六 静态,单例设计模式,继承,多态
- 黑马程序员——ios基础---练习:4
- JAVA基础6.53——多态练习