今天老师上课讲解的很好,特意记录下来便于以后复习。
多态的简单理解
* 1.什么是多态性?
* (1)同一个动作与不同的对象产生不同的行为
* (2)多态指的是一个对象的多种形态
*
* 2.多态的体现方式
* (1).使用继承:不同的子类重写父类方法后,体现出来的形式不一样
* (2)接口的实现
*
* 3.形成多态的必要条件
* (1)继承:存在子类和父类的关系
* (2)重写:子类重写了父类的方法
* (3).子类对象的多态性(重点):父类的引用指向子类的实例
*
* 4.程序分为两种状态,一个编译状态,一个是运行状态
* 对于多态来说,编译时看左边,你这个对象是定义的是什么类型就是什么类型,讲pet对象看做是Pet类型
* 运行时,看右边,真正执行的对象,也就是子类对象(Dog),执行的也是子类重写后的方法。
* 向上转型和向下转型可以这样理解
*
* 5.上转型和下转型
* (1)上转型:讲子类对象交给父类引用,可以自动转换
* (2)下转型:把父类转成子类 强制类型转换
*
* 6.对象的多态性
* (1).父类的引用指向子类的实例
* (2).在编译期间调用的父类的方法,真正执行的时候,执行的是子类的方法,
* 如果子类没有重写这个方法,依旧调用父类的方法
* (3).编译看左边,运行看右边
- 宠物类
package com.hpe.demo;
public class Pet {private String name;protected int health=100;protected int love=0;public int getLove() {return love;}public void setLove(int love) {this.love = love;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHealth() {return health;}public void setHealth(int health) {this.health = health;}public Pet(String name, int health) {super();this.name = name;this.health = health;}public Pet(String name) {super();this.name = name;}public Pet() {super();}public void eat(){System.out.println("宠物吃食");}public void info(){System.out.println("名字叫:"+this.name+",我的健康值是:"+this.health+",我和主人的亲密度:"+this.love);}}
- 狗狗类
package com.hpe.demo;
public class Dog extends Pet {private String starin;public String getStarin() {return starin;}public void setStarin(String starin) {this.starin = starin;}public Dog() {super();}public Dog(String name, String starin) {super(name);this.starin = starin;}public void eat(){super.health=super.health+3;System.out.println("狗狗吃饱了,健康值加3");}public void info(){super.info();System.out.println("我的品种是:"+this.starin);}public void sleep(){System.out.println("狗狗在呼呼大睡");}public void catchingFlyDisc(){System.out.println("狗狗正在接飞盘:");super.love=super.love+5;super.health-=10;}}
- 企鹅类
package com.hpe.demo;
public class Penguin extends Pet{private String sex;public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Penguin() {super();}public Penguin(String name,String sex) {super(name);this.sex = sex;}public void eat(){super.health=super.health+5;System.out.println("企鹅吃饱了,健康值加5");}public void info(){super.info();System.out.println(",我的性别是:"+this.sex);}public void swimming(){System.out.println("企鹅正在游泳:");super.love=super.love+5;super.health-=10;}
}
- 主人类
package com.hpe.demo;
public class Master {private String hostName;public String getHostName() {return hostName;}public void setHostName(String hostName) {this.hostName = hostName;}public void feed(Dog dog){dog.eat();}public void feed(Penguin p){p.eat();}public void play(Pet pet){if(pet instanceof Dog){Dog dog=(Dog)pet;dog.catchingFlyDisc();dog.info();}else if(pet instanceof Penguin){Penguin p=(Penguin)pet;p.swimming();p.info();}}}
- 测试类
package com.hpe.demo;public class Test {public static void main(String[] args) {Pet p=new Pet("宠物");p.eat();p.info();System.out.println("========================================");Dog dog=new Dog("小金", "金毛");dog.eat();dog.info();System.out.println("========================================");Penguin pe=new Penguin("Q妹", "雌");pe.eat();pe.info();System.out.println("========================================");Master m=new Master();m.feed(dog);m.feed(pe);System.out.println("=============================================");Pet pet=new Dog("小迪", "泰迪");pet.info();Dog dog1=(Dog)pet;dog1.sleep();System.out.println("========================================");if(pet instanceof Dog){Dog dog2=(Dog)pet;dog2.sleep();}else if(pet instanceof Penguin){Penguin pe1=(Penguin)pet;pe1.swimming();} System.out.println("================================================");Pet dog2=new Dog("小花","哈士奇");Pet pe2=new Penguin("小黑", "雄");Master m1=new Master();m1.play(dog2);m1.play(pe2);}}