Java面向对象_7.Java多态1
一、多态(多种形态)
1.概述
多态意味着允许不同类的对象对同一消息做出不同的响应。
2.分类
(1)编译时多态(设计时多态)
方法重载
(2)运行时多态
程序运行时动态决定调用哪个方法
3.必要条件
(1)满足继承关系
(2)父类引用指向子类对象
4.实体类编写
package com.hello.animal;
public class Animal {
private String name;
private int month;
public Animal() {
}
public Animal(String name, int month) {
this.name = name;
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public void eat() {
System.out.println("动物都会吃东西");
}
}
package com.hello.animal;
public class Cat extends Animal {
private double weight;
public Cat() {
}
public Cat(String name, int month, double weight) {
super(name, month);
this.weight = weight;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void run() {
System.out.println("小猫跑动");
}
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package com.hello.animal;
public class Dog extends Animal {
private String sex;
public Dog() {
}
public Dog(String name, int month, String sex) {
this.setMonth(month);
this.setName(name);
this.setSex(sex);
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sleep() {
System.out.println("狗在睡觉");
}
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
二、向上转型
package com.hello.test;
import com.hello.animal.Animal;
import com.hello.animal.Cat;
import com.hello.animal.Dog;
public class AnimalTest {
public static void main(String[] args) {
Animal one = new Animal();
Animal two = new Cat();//向上转型(隐式转型)(自动转型)
Animal three = new Dog();//向上转型(隐式转型)(自动转型)
one.eat();
two.eat();
three.eat();
/*输出:
* 动物都会吃东西
* 猫吃鱼
* 狗吃肉
*/
}
}
三、向下转型(强制类型转换)
package com.hello.test;
import com.hello.animal.Animal;
import com.hello.animal.Cat;
import com.hello.animal.Dog;
public class AnimalTest2 {
public static void main(String[] args) {
Animal two = new Cat();// 向上转型(隐式转型)(自动转型)
Cat temp = (Cat) two;
temp.eat();
temp.run();
/*
* 输出 猫吃鱼 小猫跑动
* 子类引用指向父类对象,此处必须进行强转,可以调用子类特有的方法
*/
Dog temp2 = (Dog) two;// 猫->动物->狗
temp2.eat();
temp2.sleep();
temp2.getSex();
// 运行时会异常,因为Cat不能转为Dog
// 必须满足转型条件才能强转
}
}
四、instanceof运算符
1.对象–instanceof–>类
判断左边对象是否满足右边类的实例
若满足,则返回true,否则返回false。
package com.hello.test;
import com.hello.animal.Animal;
import com.hello.animal.Cat;
import com.hello.animal.Dog;
public class AnimalTest2 {
public static void main(String[] args) {
Animal two = new Cat();// 向上转型(隐式转型)(自动转型)
if (two instanceof Cat) {
Cat temp = (Cat) two;
temp.eat();
temp.run();
/*
* 输出 猫吃鱼 小猫跑动 子类引用指向父类对象,此处必须进行强转,可以调用子类特有的方法
*/
}
if (two instanceof Dog) {
Dog temp2 = (Dog) two;// 猫->动物->狗
temp2.eat();
temp2.sleep();
temp2.getSex();
// 运行时会异常,因为Cat不能转为Dog
// 必须满足转型条件才能强转
}
if (two instanceof Object) {
System.out.println("two可以转换为Object类型");//会输出
}
}
}
static方法不允许被子类重写
同名的两个父子类静态方法其实时两个不同的方法
五、抽象类
1.Java中使用抽象类,限制实例化
public abstract class Animal{
}
2.不允许实例化,可以通过向上转型指向子类实例
3.应用场景
某个父类只是知道其子类应该包含怎样的方法,但无法准确知道这些子类如何实现这些方法。
4.作用
(1)限制子类设计随意性
(2)避免无意义父类的实例化
六、抽象方法
1.public abstract void eat();
(1)不能有方法体
(2)子类必须重写抽象方法
(3)抽象类继承抽象类可以不重写抽象方法
2.使用价值
可以提醒子类重写抽象方法
3.注意
包含抽象方法的类一定是抽象类
但抽象类中可以没有抽象方法
static、final、private都不可与abstract同用