一、多态的概述
- 事物存在多种形态
2.多态的前提
- 要有继承关系
- 要有方法重写
- 要有父类引用指向子类对象
3.代码体现
class Demo1_Polymorphic {
public static void main(String[] args) {
Cat c = new Cat();
c.eat();
//父类引用指向子类对象
Animal a = new Cat();
a.eat();
}
}
class Animal {
public void eat() {
System.out.println("动物吃饭");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
}
输出:
猫吃鱼
猫吃鱼
4.多态中成员变量访问特点
- 成员变量
编译看左边(父类)运行看左边(父类)
class Demo2_Polymorphic {
public static void main(String[] args) {
//父类引用指向子类对象
Father f = new Son();
System.out.println(f.num);
}
}
class Father {
int num = 10;
}
class Son extends Father {
int num = 20;
}
输出:10

5.多态中成员方法访问特点
- 成员方法
编译看左边(父类),运行看右边(子类)。动态绑定
class Demo2_Polymorphic {
public static void main(String[] args) {
//父类引用指向子类对象
Father f = new Son();
System.out.println(f.num);
Son s = new Son();
System.out.println(s.num);
Father f1 = new Son();
f1.print();
}
}
class Father {
int num = 10;
public void print() {
System.out.println("father");
}
}
class Son extends Father {
int num = 20;
public void print() {
System.out.println("son");
}
}
输出:
10
20
son
6.多态中静态成员方法访问特点
- 静态方法
- 编译看左边(父类),运行看左边(父类)。
- (静态和类相关,算不上重写,所以,访问还是左边的)
只有非静态的成员方法,编译看左边,运行看右边
class Demo2_Polymorphic {
public static void main(String[] args) {
Father f = new Son();
//相当于是Father.method()
f.method();
}
}
/*
静态方法
编译看左边(父类),运行看左边(父类)。
(静态和类相关,算不上重写,所以,访问还是左边的)
只有非静态的成员方法,编译看左边,运行看右边
*/
class Father {
public static void method() {
System.out.println("father static method");
}
}
class Son extends Father {
public static void method() {
System.out.println("son static method");
}
}
7.多态中向上转型与向下转型
- 一定先有向上转型,才有向下转型
基本数据类型自动类型提升和强制类型转换
int i = 10;
byte b = 20;
i = b; //自动类型提升
b = (byte)i; //强制类型转换
class Demo3_SuperMan {
public static void main(String[] args) {
//父类引用指向子类对象,超人提升为了人
Person p = new SuperMan();
//父类引用指向子类对象就是向上转型
System.out.println(p.name);
p.谈生意();
//向下转型
SuperMan sm = (SuperMan) p;
sm.fly();
}
}
class Person {
String name = "John";
public void 谈生意() {
System.out.println("谈生意");
}
}
class SuperMan extends Person {
String name = "superMan";
public void 谈生意() {
System.out.println("谈几个亿的大单子");
}
public void fly() {
System.out.println("飞出去救人");
}
}
输出:
John
谈几个亿的大单子
飞出去救人
8.多态的好处与弊端
多态的好处
- 提高了代码的维护性(继承保证)
- 提高了代码的扩展性(由多态保证)
- 多态的好处
- 可以当作形式参数,可以接收任意子类对象
多态的弊端
- 不能使用子类的特有属性和行为。
class Demo4_Animal {
public static void main(String[] args) {
Cat c1 = new Cat();
c1.eat();
method(new Cat());
method(new Dog());
}
//Cat c = new Dog();狗是一只猫,这是错误的
public static void method(Cat c) {
c.eat();
}
public static void method(Dog d) {
d.eat();
}
}
class Animal {
public void eat() {
System.out.println("动物吃饭");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("狗吃肉");
}
public void lookHome() {
System.out.println("看家");
}
}
输出:
猫吃鱼
猫吃鱼
狗吃肉
class Demo4_Animal {
public static void main(String[] args) {
method(new Cat());
method(new Dog());
// 开发的是很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为
//Animal a = new Cat();
}
//如果把狗强转成猫就会出现类型转换异常,ClassCastException
public static void method(Animal a) { //当作参数的时候用多态最好,因为扩展性强
//关键字 instanceof 判断前边的引用是否是后边的数据类型
if (a instanceof Cat) {
Cat c = (Cat) a;
c.eat();
c.catchMouse();
} else if (a instanceof Dog) {
Dog d = (Dog) a;
d.eat();
d.lookHome();
} else {
a.eat();
}
}
}
class Animal {
public void eat() {
System.out.println("动物吃饭");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("狗吃肉");
}
public void lookHome() {
System.out.println("看家");
}
}
输出:
猫吃鱼
抓老鼠
狗吃肉
看家
9.多态中的注意事项
class Test1_Polymorphic {
public static void main(String[] args) {
//编译看左边,运行看右边
Fu f = new Zi();
f.show();
//f.method();//因为父类中没有此方法,编译会报错的
}
}
class Fu {
public void show() {
System.out.println("fu show");
}
}
class Zi extends Fu {
public void show() {
System.out.println("zi show");
}
public void method() {
System.out.println("zi method");
}
}
class Test2_Polymorphic {
public static void main(String[] args) {
A a = new B();
a.show();
//编译看左边,运行看右边,父类B有show()方法,编译通过,调用子类C的show()方法
B b = new C();
b.show();
}
}
class A {
public void show() {
show2();
}
public void show2() {
System.out.println("我");
}
}
class B extends A {
public void show() {
show2();
}
public void show2() {
System.out.println("爱");
}
}
class C extends B {
public void show() {
super.show();
}
public void show2() {
System.out.println("你");
}
}
输出:
爱
你
本文介绍了Java中的多态概念,包括多态的前提条件——继承和方法重写,以及多态在成员变量、成员方法、静态成员方法访问上的特点。讨论了多态带来的好处,如代码维护性和扩展性提升,同时也指出了其弊端,如无法访问子类特有属性和行为。还涵盖了向上转型和向下转型的操作,并给出了多态中应注意的事项。
1513

被折叠的 条评论
为什么被折叠?



