标签:alt log cas one min setname hid div opened
1.1 多态:一个事物的多种形态
1.2 多态的表现
行为(方法)多态:重写和重载。
引用多态(动态绑定):编译时的类型和运行时不一致的这种现象
例:父类 = 子类
Pet p = new Dog();
向上转型;父类=子类
例:Pet p = new Dog();
向下转型:子类 = (子类)父类
例: Dog d = (Dog)p;
1 public abstract class Pet { 2 private String name; 3 private int health=100; 4 private int age; 5 private int love=50; 6 public Pet(){ 7 8 } 9 public Pet(String name,int health,int age,int love){ 10 this.name = name; 11 this.health = health; 12 this.age = age; 13 this.love = love; 14 } 15 public String getName(){ 16 return name; 17 } 18 public void setName(String name){ 19 this.name= name; 20 } 21 public int getHealth(){ 22 return health; 23 } 24 public void setHealth(int health){ 25 this.health = health; 26 } 27 public int getAge(){ 28 return age; 29 } 30 public void setAge(int age){ 31 this.age = age; 32 } 33 public int getLove(){ 34 return love; 35 } 36 public void setLove(int love){ 37 this.love = love; 38 } 39 public void ji(){ 40 System.out.println("宠物的自白:我的名字叫:"+this.name+",年龄为:"+this.age+"和主人的亲密度为:"+this.love); 41 } 42 public abstract void eat(); 43 }
1 public class Dog extends Pet{ 2 private String strain; 3 public Dog(){ 4 5 } 6 public Dog(String name,int health,int age,int love,String strain){ 7 super(name,health,age,love); 8 this.strain = strain; 9 } 10 public void catchingFlyDisc(){ 11 System.out.println("接飞盘"); 12 int health = getHealth(); 13 health = health-10; 14 setHealth(health); 15 16 int love = getLove(); 17 love = love+5; 18 setLove(love); 19 } 20 public void eat(){ 21 System.out.println("狗狗吃鱼"); 22 } 23 public String getStrain(){ 24 return strain; 25 } 26 public void setStrain(String strain){ 27 this.strain = strain; 28 } 29 30 }
1 public class Penguin extends Pet { 2 private String sex; 3 public Penguin(){ 4 5 } 6 public Penguin(String name,int health,int age,int love,String sex){ 7 super(name,health,age,love); 8 this.sex = sex; 9 } 10 public void swimming(){ 11 System.out.println("游泳"); 12 int health = getHealth(); 13 health = health-10; 14 setHealth(health); 15 16 int love = getLove(); 17 love = love+5; 18 setLove(love); 19 } 20 public void eat(){ 21 System.out.println("企鹅喝水"); 22 } 23 public String getSex(){ 24 return sex; 25 } 26 public void setSex(String sex){ 27 this.sex = sex; 28 } 29 30 }
1 public class Master { 2 public void feed(Pet p){ 3 p.eat(); 4 } 5 public void play(Pet p){ 6 if(p instanceof Dog ){ 7 Dog d = (Dog) p; 8 d.catchingFlyDisc(); 9 }else if(p instanceof Penguin){ 10 Penguin pe = (Penguin) p; 11 pe.swimming(); 12 } 13 } 14 }
1 public class TestPMPD { 2 public static void main(String[] args){ 3 Master m= new Master(); 4 //向下转型 5 Pet p = new Dog(); 6 Dog d = (Dog) p; 7 m.feed(d); 8 // Pet p = new Penguin(); 9 // m.feed(p); 10 } 11 }
运行结果为:
注意:java.lang.ClassCastException:类型转换异常
父类的运行时类型跟要转成的子类的类型不一致
注意:引用多态存在继承关系
语法: 对象A instanceof 类型B
判断对象A的类型是否是类型B,是就返回true,否则返回false
例:d instanceof Dog
a.使用父类作为方法形参实现多态
public void play(Pet p){...}
b.使用父类作为方法返回值实现多态
public Pet getPet(int type){...}
标签:alt log cas one min setname hid div opened
原文地址:http://www.cnblogs.com/-chaoming/p/7019784.html