一、多态
1、概念
java中对象的三大特征为:封装、继承、多态。其中多态源于继承,即没有继承也就没有多态。
所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个类中实现的方法,必须在由程序运行期间才能决定。因为在程序运行时才确定具体的类,这样,不用修改源程序代码,就可以让引用变量绑定到各种不同的类实现上,从而导致该引用调用的具体方法随之改变,即不修改程序代码就可以改变程序运行时所绑定的具体代码,让程序可以选择多个运行状态,这就是多态性。
2、里氏替换原则
想要理解多态,首先要明白什么是里氏替换原则。
里氏替换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一。 里氏替换原则中说,任何基类可以出现的地方,子类一定可以出现。
3、多态的作用是什么?
1. 降低程序的耦合度,提高程序的扩展力。
2. 能使用多态尽量使用多态。
3. 父类型引用指向子类型对象。
4、向下和向上转型
设定Animal 是Person的父类:
向上转型:Animal animal = new Person();
值得注意的是:
- 声明的animal接收了Person类的对象,但animal不能使用Person类中独有的方法。
- 子类引用不能指向父类对象,如(Persom person = new Animal )就是错误的。
向下转型:Person persion = (Person)animal;
值得注意的是:
- 向下转型的前提是父类对象指向的是子类对象。
- 向下转型只能转型为本类对象。
二、数据传递
java中的数据传递只支持值传递:
- 值类型传递
值类型包括八种基本数据类型和枚举(ENUM)
传递方式是通过复制栈内存中的值进行传递。
C++,C语言即支持值传递,也支持引用类型传递
- 引用类型传递
引用类型的值传递会改变
三、练习
//父类:宠物类
public class Pet {
private String name;
int health;
int love;
public void talk() {
System.out.printf("我是叫%s,健康值为:%s,亲密度为:%s\n",name,health,love);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
if (health>0&&health<100) {
this.health = health;
}else if (health<=0) {
System.out.println("您的宠物已经去世!");
this.health = 0;
}else{
System.out.println("您的宠物已经是最健康状态!");
this.health = 100;
}
}
public int getLove() {
return love;
}
public void setLove(int love) {
if (love>0&&love<100) {
this.love = love;
}else if (love<=0) {
System.out.println("您的宠物已经抛弃了你!");
this.love = 0;
}else{
System.out.println("您的宠物非常喜爱你!");
this.love = 100;
}
}
}
//子类1:狗类
public class Dog extends Pet{
private String strain;
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public void dogTalk(Pet pet) {
setHealth(pet.health);
setLove(pet.love);
System.out.print("我的品种是"+strain+",");
talk();
}
public void toHospital(int a,Pet pet) {
for (int i = 1; i <= a; i++) {
pet.health +=6;
pet.love +=3;
}
}
public void dogDarts() {
System.out.println("狗狗接飞镖。");
}
}
//子类:企鹅类
public class Penguin extends Pet{
String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void penguinTalk(Pet pet) {
setHealth(pet.health);
setLove(pet.love);
System.out.print("我的性别为"+sex+",");
talk();
}
public void toHospital(int a,Pet pet) {
for (int i = 1; i <= a; i++) {
pet.health +=6;
pet.love+=3;
}
}
public void penguinFish() {
System.out.println("企鹅在吃鱼。");
}
}
//测试类:主方法
import java.util.Scanner;
public class Work01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("欢迎您来到宠物店!");
System.out.print("请输入要领养宠物的名字:");
String name = input.next();
System.out.print("请选择要领养的宠物类型:(1、狗狗 2、企鹅):");
String pt = null;
int type;
while (true) {
type = input.nextInt();
if (type == 1) {
pt = "狗狗";
break;
} else if (type == 2) {
pt = "企鹅";
break;
} else {
System.out.println("输入错误请重新输入.");
}
}
System.out.print("请输入" + pt + "的健康值(1~100之间):");
int health = input.nextInt();
System.out.print("请输入" + pt + "的亲密度(1~100之间):");
int love = input.nextInt();
Pet pet = new Pet();
pet.setName(name);
pet.setLove(love);
pet.setHealth(health);
Dog dog = new Dog();
Penguin penguin = new Penguin();
int a = 0;
if (type == 1) {
System.out.print("请输入狗的品种:");
String strain = input.next();
dog.setStrain(strain);
dog.toHospital(a,pet);
dog.dogTalk(pet);
} else if (type == 2) {
System.out.print("请输入企鹅的性别:");
String sex = input.next();
penguin.setSex(sex);
penguin.toHospital(a,pet);
penguin.penguinTalk(pet);
}
while (true) {
System.out.print("是否选择去医院:(y/n)");
String c = input.next();
if (c.equals("y")) {
System.out.print("请输入去医院的次数:");
a = input.nextInt();
if (type == 1) {
dog.toHospital(a,pet);
dog.dogTalk(pet);
} else if (type == 2) {
penguin.toHospital(a,pet);
penguin.penguinTalk(pet);
}
continue;
} else if (c.equals("n")) {
break;
} else {
System.out.println("输入错误请重新输入.");
}
}
//第二题
Pet pe = new Penguin();
if (pe instanceof Penguin) {
Penguin p = (Penguin) pe;
p.penguinFish();
}
Pet t = new Dog();
if (t instanceof Dog) {
Dog d = (Dog) t;
d.dogDarts();
}
}
}