当前位置: 首页 > news >正文

多态(全)

一、多态

1.什么是多态

通俗来说,就是多种形态,具体点就是去完成某个⾏为,当不同的对象去完成时会产⽣出不同的状态,有不同的结果。

2.多态是如何实现的

多态的实现需要满足以下的条件:

(1)必须在继承体系下

(2)⼦类必须要对⽗类中⽅法进⾏重写

(3)通过⽗类的引⽤调⽤重写的⽅法

public class Animal {String name;int age;public Animal(String name, int age){this.name = name;this.age = age;}public void eat(){System.out.println(name + " 吃饭 ");}
}public class Cat extends Animal{public Cat(String name, int age){super(name, age);}@Overridepublic void eat(){System.out.println(name+" 吃⻥ ~~~");}
}public class Dog extends Animal {public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(name + " 吃⻣头 ~~~");}
}public class TestAnimal {public static void eat(Animal a){a.eat();}public static void main(String[] args) {Cat cat = new Cat(" 元宝 ",2);Dog dog = new Dog("⼩七 ", 1);eat(cat);eat(dog);}
}运⾏结果:
元宝吃⻥~~~
元宝正在睡觉
⼩七吃⻣头~~~
⼩七正在睡觉

当类的调⽤者在编写 eat 这个⽅法的时候,参数类型为Animal(⽗类),此时在该⽅法内部并不知道, 也不关注当前的a引⽤指向的是哪个类型(哪个⼦类)的实例.此时a这个引⽤调⽤eat⽅法可能会有多种 不同的表现(和a引⽤的实例相关),这种⾏为就称为多态.

二、重写(覆盖/覆写)

⽅法重写的规则:

(1)⼦类在重写⽗类的⽅法时,⼀般必须与⽗类⽅法原型⼀致:返回值类型 ⽅法名 (参数列表) 要完全⼀致

(2)被重写的⽅法返回值类型可以不同,但是必须是具有⽗⼦关系的

(3)访问权限不能⽐⽗类中被重写的⽅法的访问权限更低。(public>protected>   >private)

例如:如果⽗类⽅法被public修饰,则⼦类中重写该⽅法就不能声明为protected

(4)⽗类被final、static、private修饰的⽅法、构造⽅法都不能被重写。

(5)重写的⽅法,可以使⽤ @Override 注解来显式指定.有了这个注解能帮我们进⾏⼀些合法性校验. 例如不⼩⼼将⽅法名字拼写错了(⽐如写成aet),那么此时编译器就会发现⽗类中没有aet⽅法,就 会编译报错,提⽰⽆法构成重写.

public class Animal {public String name;public int age;public Animal(String name, int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name +" 正在吃饭....");}
}public class Bird extends Animal{public Bird(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.name +" 正在吃鸟粮....");}public void fly() {System.out.println(this.name+" 正在飞....");}
}public class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println(this.name +" 正在吃狗粮....");}public void bark() {System.out.println(this.name+" 正在汪汪叫....");}
}

2.动态绑定

也称为后期绑定(晚绑定),即在编译时,不能确定⽅法的⾏为,需要等到程序运⾏时,才能 够确定具体调⽤那个类的⽅法。

public class Animal {public String name;public int age;public Animal(String name, int age) {this.name = name;this.age = age;}public void eat() {System.out.println(this.name +" 正在吃饭....");}
}public class Dog extends Animal{public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {//子类重写父类的方法System.out.println(this.name +" 正在吃狗粮....");}public void bark() {System.out.println(this.name+" 正在汪汪叫....");}
}public static void main(String[] args) {/*Dog dog = new Dog("旺财",10);Animal animal = dog;*/Animal animal = new Dog("旺财",10);//向上转型animal.eat();//这里调用了父类自己的方法,但是发生了动态绑定,结果是子类自己的方法}
}

动态绑定发生的条件:

(1)发生了向上转型

(2)子类进行了重写父类的方法

(3)通过父类引用  调用了这个重写的方法

三、向上转型

向上转型:实际就是创建⼀个⼦类对象,将其当成⽗类对象来使⽤

 Animal animal = new Cat("元宝",2);

1.直接赋值

 Animal animal = new Cat("元宝",2);

2.参数传递

public static void func(Animal animal){animal.eat();
}pcblic static void main(String[] args){Dog dog = new Dog("旺财",10);Bird bird = new Bird("啾啾",2);func(dog);func(bird);
}

3.返回值传递

public static Animal func2(int a) {if(a == 1) {return new Dog("旺财",10);}else {return new Bird("啾啾",19);}
}

向上转型的优点:让代码实现更简单灵活。

向上转型的缺陷:不能调⽤到⼦类特有的⽅法。

四、向下转型

将⽗类引⽤再还原为⼦类对象即可,即向下转换。

public static void main(String[] args) {Animal animal = new Dog("旺财",10);if(animal instanceof Dog) {Dog dog = (Dog) animal;dog.bark();System.out.println("animal 引用的是 Dog这样的对象");//111}else {System.out.println("animal 引用的不是 Dog这样的对象");}if(animal instanceof Bird) {Bird bird = (Bird) animal;bird.fly();System.out.println("animal 引用的是 Bird这样的对象");}else {System.out.println("animal 引用的不是 Bird这样的对象");//22222
}

#注:

(1)由于这里的父类不一定是子类,所以要强转

(2)向下转型不安全,为了安全,引入了instanceof,如果该表达式为true,则可以安全转换。

五、多态的优缺点

1.降低了圈复杂度(减少了if语句、循环语句的使用)

 public static void drawShapes() {Rect rect = new Rect();Cycle cycle = new Cycle();Flower flower = new Flower();String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};for (int i = 0; i < shapes.length; i++) {String shape = shapes[i];if(shape.equals("cycle")) {cycle.draw();}else if(shape.equals("rect")) {rect.draw();}else {flower.draw();}}}
 public static void drawShapes2() {Rect rect = new Rect();Cycle cycle = new Cycle();Flower flower = new Flower();Shape[] shapes = {rect,cycle,rect,cycle,flower};//Shape[] shapes = {new Rect(),new Cycle(),new Rect(),new Cycle(),new Flower()};for(Shape shape : shapes) {shape.draw();}}

对比上面两串代码,我们不难发现,第二个代码明显更加的高级、易看懂

2.可扩展能⼒更强

如果要新增⼀种新的形状,使⽤多态的⽅式代码改动成本也⽐较低.

 class Triangle extends Shape {@Overridepublic void draw() {System.out.println("△ ");}}

相关文章:

  • 企业级实战之Iptables防火墙案例分析
  • 11. MySQL事务管理(上)
  • 极客大挑战 2019 EasySQL 1(万能账号密码,SQL注入,HackBar)
  • 3.spring基础入门(三)
  • 打卡day44
  • typescript中的type如何使用
  • 信息学奥赛一本通 1570:【例 2】能量项链 | 1843:【06NOIP提高组】能量项链 | 洛谷 P1063 [NOIP 2006 提高组] 能量项链
  • MySQL 索引:为使用 B+树作为索引数据结构,而非 B树、哈希表或二叉树?
  • React-native实战系列
  • 论文速读《VideoMimic:通过视觉模仿实现人形机器人感知控制》
  • 玩转Docker | 使用Docker部署Qwerty Learner英语单词学习网站
  • 第八部分:第三节 - 事件处理:响应顾客的操作
  • AXPM11584:颠覆传统,发现新可能
  • 省赛中药检测模型调优
  • 电路图识图基础知识-降压启动(十五)
  • Java面试题及答案整理( 2025年最新版,持续更新...)
  • 理解继承与组合的本质:Qt 项目中的设计选择指南
  • 《射频识别(RFID)原理与应用》期末复习 RFID第一章 射频识别技术概论(知识点总结+习题巩固)
  • 前端css外边距塌陷(Margin Collapse)现象原因和解决方法
  • 核心机制:拥塞控制
  • 80后陈某做盗版视频网站/搜索推广开户
  • 南阳做网站哪个好/营销方案设计思路
  • 简单手机网站如何制作/上海做推广的引流公司
  • 杭州做企业网站/百度贴吧热线客服24小时
  • 免费天眼查公司查询/深圳网站优化公司哪家好
  • 如何打开网站/知名的seo快速排名多少钱