多态

一,多态的定义,

不同的方法对于同一个操作而做出的不同结果,

二,多态的三种实现方案,

方案一:父类  普通类            方法a()

             子类   普通类 继承父类  方法a()

    父类 父类对象=new 子类型();
   父类对象.a()

  方案二:父类  抽象类   抽象方法a()

                子类  普通类   重写抽象方法a()
   父类 父类对象=new 子类型();
   父类对象.a()

  方案三:使用接口实现多态

三,父类作为方法参数,

public class Master {
    public Pet getPet(String typeId ){
           … …         
    }
}

4.父类类型作为方法返回值   简单工厂
 public class AnimalFactory
 {
    public static Animal getInstance(String type){
       Animal animal=null;

       return animal;
    }
 }

 5.单例模式
   条件1:构造私有的

   条件2:private 静态的成员变量,存储唯一的对象
     private static Student stu=null;

   条件3:静态方法,提供给外界用来获取唯一实例的一个通道。
 
  懒汉: private static Student stu=null;
  
  饿汉: private static Student stu=new Student();      

6.向上转型(子转成父) 隐式类型转换
  Parent parent=new Child();

向下转型(父转子)
  Parent parent=new Child();

 Child child= (Child)parent;

7.使用多态的好处,

减少类中的代码的

增加代码的可扩展性和可维护性,

posted @ 2018-03-10 15:02  blond  阅读(170)  评论(0编辑  收藏  举报