42-多态







多态

  • 动态编译: 类型: 可扩展性

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式

  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

  • instanceof 类型转换


父类的引用指向子类

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Person_01;
        //new Student_01;
        //可以指向的引用类型就不确定
        Student_01 s1 = new Student_01();
        Person_01 s2 = new Student_01();
        Object s3 = new Student_01();
    }
}

示例


main 方法

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Person_01;
        //new Student_01;
        //可以指向的引用类型就不确定
        Student_01 s1 = new Student_01();
        Person_01 s2 = new Student_01();
        Object s3 = new Student_01();
        s2.run();
        s1.run();  //子类重写了父类的方法,执行子类的方法
    }
}

父类 Person

public class Person_01 {
    public void run() {
        System.out.println("Person的run执行了");
    }
}

子类 Student

public class Student_01 extends Person_01 {
    public void run() {
        System.out.println("Student的run执行了");
    }
}

但是如果

main 方法

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Person_01;
        //new Student_01;
        //可以指向的引用类型就不确定
        //Student 能调用的方法都是自己或者继承父类的
        Student_01 s1 = new Student_01();
        //Person 父类型,可以指向子类。但是不能调用子类独有的方法
        Person_01 s2 = new Student_01();
        Object s3 = new Student_01();
        s2.eat();
        s1.run();  //子类重写了父类的方法,执行子类的方法
    }
}

父类 Person

public class Person_01 {
    public void run() {
        System.out.println("Person的run执行了");
    }
}

子类 Student

public class Student_01 extends Person_01 {
    public void run() {
        System.out.println("Student的run执行了");
    }
    public void eat() {
        System.out.println("Student的eat执行了");
    }
}

以上代码 s2.eat(); 会报错,因为引用类型为 PersonPerson_01 s2 = new Student_01),而 Person 类中没有 eat 方法,所以会报错。

对象能执行哪些方法,主要看对象左边的类型,和右边的关系。

强制转换


main 方法

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Person_01;
        //new Student_01;
        //可以指向的引用类型就不确定
        Student_01 s1 = new Student_01();
        Person_01 s2 = new Student_01();
        Object s3 = new Student_01();
        ((Student_01) s2).eat();
        s1.run();  //子类重写了父类的方法,执行子类的方法
    }
}

父类 Person

public class Person_01 {
    public void run() {
        System.out.println("Person的run执行了");
    }
}

子类 Student

public class Student_01 extends Person_01 {
    public void run() {
        System.out.println("Student的run执行了");
    }
    public void eat() {
        System.out.println("Student的eat执行了");
    }
}

强制将 Person_01 类型转换成 Student_01 类型

注意事项


  1. 多态是方法的多态,属性没有多态

  2. 父类和子类,必须要有联系。否则会报类型转换异常。(classCastException)

  3. 存在条件: 继承关系,方法需要重写,父类引用指向子类对象

    1. static: 方法,属于类,不属于实例

    2. final : 常量

    3. private 方法:


评论

  1. 博主
    4 年前
    2022-3-29 18:42:29

    所谓 “多态”,就是定义的引用变量所指向的具体类型和通过改引用变量发出的方法调用在编写代码时并不确定,而在程序运行的时候才能确定。即一个引用变量会指向哪一个类的实例对象,引用变量发出的方法调用在哪个类中实现的方法。必须在程序运行的时候才能决定。程序运行时才确定具体的类,这样不用修改程序源代码,就可以让引用变量绑定到各种不同的类上。从而使该引用调用的具体方法随之改变。即不修改程序代码就可以改变程序运行时所绑定的具体代码。让程序可以选择多个运行状态,这就是多态性。

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇