黑马程序员--多态练习(手机工厂)
2013-12-10 20:30
197 查看
---------------------- 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; 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) { } }
相关文章推荐
- 黑马程序员--多态练习(手机工厂)
- 黑马程序员——Java练习笔记——多态
- 黑马程序员—java基础_异常多态练习
- Java 练习(多态,instanceof)
- 第三讲.继承,完整初始化方法,遍历构造器,多态(代码) 另附植物大战僵尸练习
- 面向对象之多态及类工厂
- 黑马程序员-JavaAPI-16天-6(Map练习)
- 黑马程序员_day08 面向对象(多态,object类和equals()和toString())
- 黑马程序员——多态、内部类
- 关于类,接口,多态,工厂方法的实例
- 多态练习
- 黑马程序员-面向对象 多态,内部类
- 黑马程序员----java中的多态
- 黑马程序员--多态
- 黑马程序员--面向对象学习---接口、多态、内部类、异常
- 黑马程序员_内部类和异常的练习
- #static,final,抽象,接口,多态,内部类,单子和工厂设计模式
- 黑马程序员_NSDate练习
- 黑马程序员--多态
- 黑马程序员_关于多态的应用举例