马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.多态的概念
普通来说,就是多种形态, 具体点就是去完成某个行为,当不同的对象去完成时会产生出不同 的状 态 PS:我们以打印机的例子来说,不同的打印机去打印同一个文件会得到不同的结果 2.多态实现的条件
1. 必须在继承体系下 2. 子类必须要对父类中方法进行重写 3. 通过父类的引用调用重写的方法 多态体现:在代码运行时,当传递不同类对象时,会调用对应类中的方法(下面通过一个简朴的代码来演示) - package test3;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class Animal {
- private int age;
- String name;
- public Animal(int age, String name) {
- this.age = age;
- this.name = name;
- }
- public void eat() {
- System.out.println(name + " 吃饭");
- }
- }
复制代码 - package test3;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:当Cat在调用父类的收就会重写父类
- */
- public class Cat extends Animal {
- public Cat(String name, int age) {
- super(age, name);
- }
- @Override
- public void eat() {
- System.out.println(name + "吃鱼");
- }
- }
复制代码
- package test3;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class Dog extends Animal {
- public Dog(int age, String name) {
- super(age, name);
- }
- @Override
- public void eat() {
- System.out.println(name + "吃骨头");
- }
- }
复制代码
- package test3;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class TestAnimal {
- /**
- * @description:调用 animal.eat() 时,实际执行的将是传入对象的 eat 方法,取决于对象的具体类型(Cat 或 Dog),体现了多态的特性
- * @author: chenqj
- * @date: 2024/9/25 9:51
- * @param animal
- * @return: void
- **/
- public static void eat(Animal animal) {
- animal.eat();
- }
- public static void main(String[] args) {
- Cat cat = new Cat("小黄",10);
- Dog dog = new Dog(10,"小白");
- eat(cat);
- eat(dog);
- }
- }
复制代码 PS:顺便在这里解说一下重写和重载的区别
即:方法重载是一个类的多态性表现,而方法重写是子类与父类的一种多态性表现。
静态绑定 :也称为前期绑定 ( 早绑定 ) ,即在编译时, 根据用户所传递实参范例 就确定了具体调用谁人方法。范例代 表函数重载。 动态绑定 :也称为后期绑定 ( 晚绑定 ) ,即在编译时,不能确定方法的行为,必要 等到程序运行时 ,才能够确定具体 调用谁人类的方法。 3.向上转型
向上转型:实际就是创建一个子类对象,将其当成父类对象来使用
语法格式:父类范例 对象名 = new 子类范例() (这是直接赋值的写法,比如传参,返回值这些都可以向上转型)
//以上面的代码为例
- Animal animal = new Cat("小黑",10); -----直接赋值
- animal.eat();
复制代码 //此时是调用的父类Animal中的eat方法
===============================================
方法传参:形参为父范例引用,可以吸收任意子类的对象 - public static void eat(Animal animal) {
- animal.eat();
- }
复制代码 ==========================================
作返回值:返回任意子类对象 public static Animal eat() { - Cat cat = new Cat("小黄",10);
- return cat;
- }
复制代码 标题:那么向上转型有什么用呢?
其实用处很大,特殊是对于以后代码复杂的时候,如果你想扩展的时候应该怎么解决呢?
以上面的代码为例,如果子类想新增一个睡觉的功能,此时我们只必要在父类中新增这么一个功能就好了,不必要修改原有的代码逻辑
Animal:
缺点: 不能调用到子类特有的方法
子类特有的方法:
此时向上转型的过程中是无法调用的
4.向下转型
将一个子类对象经过向上转型之后当成父类方法使用,再无法调用子类的方法,但有时候可能必要调用子类特有的 方法,此时: 将父类引用再还原为子类对象即可 ,即向下转换
接着上面向上转型的缺陷,向下转型确实是可以解决这个标题(但是不安全,雷同于一个大范围转换为小范围,会有数据的丧失)
向下转型用的比较少,而且不安全,万一转换失败,运行时就会抛非常。 Java 中为了提高向下转型的安全性,引入 了 instanceof ,如果该表达式为 true ,则可以安全转换。 PS:此时animal中引用的是Cat对象 以是:+结果肯定是报错的(范例转换错误) 安全的写法:
5.多态的优点
我们先看这样一段代码:
- package test4;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class Shape {
- public void draw() {
- System.out.println("画图形!");
- }
- }
- class Rect extends Shape{
- @Override
- public void draw() {
- System.out.println("♦");
- }
- }
- class Cycle extends Shape{
- @Override
- public void draw() {
- System.out.println("●");
- }
- }
- class Flower extends Shape{
- @Override
- public void draw() {
- System.out.println("❀");
- }
-
- public static void main(String[] args) {
- Rect rect = new Rect();
- Cycle cycle = new Cycle();
- Flower flower = new Flower();
- String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};
- for (String shape : shapes) {
- if (shape.equals("cycle")) {
- cycle.draw();
- } else if (shape.equals("rect")) {
- rect.draw();
- } else if (shape.equals("flower")) {
- flower.draw();
- }
- }
- }
- }
复制代码 使用多态优化事后:
- package test4;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class Shape {
- public void draw() {
- System.out.println("画图形!");
- }
- }
- class Rect extends Shape{
- @Override
- public void draw() {
- System.out.println("♦");
- }
- }
- class Cycle extends Shape{
- @Override
- public void draw() {
- System.out.println("●");
- }
- }
- class Flower extends Shape {
- @Override
- public void draw() {
- System.out.println("❀");
- }
- public static void main(String[] args) {
- // Rect rect = new Rect();
- // Cycle cycle = new Cycle();
- // Flower flower = new Flower();
- // String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};
- // for (String shape : shapes) {
- // if (shape.equals("cycle")) {
- // cycle.draw();
- // } else if (shape.equals("rect")) {
- // rect.draw();
- // } else if (shape.equals("flower")) {
- // flower.draw();
- // }
- // }
- //
- // }
- Shape[] shapes = {new Cycle(), new Rect(), new Cycle(),
- new Rect(), new Flower()};
- for (Shape shape : shapes) {
- shape.draw();
- }
- }
- }
复制代码 代码的简便度显而易见
6.多态的缺点
代码的运行效率降低 1. 属性没有多态性 当父类和子类都有同名属性的时候,通过父类引用,只能引用父类自己的成员属性 2. 构造方法没有多态性(看一段代码理解) - package test5;
- /**
- * @Author: chenqj5
- * @CreateTime: 2024/09/25
- * @Description:
- */
- public class B {
- public B() {
- // do nothing
- func();
- }
- public void func() {
- System.out.println("B.func()");
- }
- }
- class D extends B {
- private int num = 1;
- @Override
- public void func() {
- System.out.println("D.func() " + num);
- }
- public static void main(String[] args) {
- D d = new D();
- }
- }
复制代码 结果:
PS:
Java 的构造器执行流程是:
- 起首,父类构造器执行。
- 然后,才会执行子类的构造器和初始化子类的实例变量。
- 构造 D 对象的同时, 会调用 B 的构造方法
- B 的构造方法中调用了 func 方法 , 此时会触发动态绑定 , 会调用到 D 中的 func(由于在 B 的构造器中调用了 D 的 func() 方法,而此时 D 的构造器还未执行, num 变量仍然处于未初始化状态)
- 此时 D 对象自身还没有构造 , 此时 num 处在未初始化的状态 , 值为 0. 如果具备多态性, num 的值应该是 1.
- 以是在构造函数内,尽量避免使用实例方法,除了 final 和 private 方法。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |