• Java面向对象——多态


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

    · 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)。

    · 多态存在的条件:

      1. 有继承关系

      2. 子类重写父类的方法;

      3. 父类引用指向子类对象

    多态注意事项:
      1. 多条是方法的多态,属性没有多态;

      2. 父类和子类,有联系    类型转换异常:ClassCastException!

      注意,以下方法没有多态:
      1. static方法,属于类,不属于实例,所以该方法不能被重写;

      2. final  常量;无法重写;

      3. private方法  私有;无法重写。


    代码演示:

    1. public class Person {
    2. public void run(){
    3. System.out.println("run");
    4. }
    5. }
    1. public class Student extends Person{
    2. @Override
    3. public void run() {
    4. System.out.println("son");
    5. }
    6. public void eat(){
    7. System.out.println("eat");
    8. }
    9. }
    1. import oop.demo06.Person;
    2. import oop.demo06.Student;
    3. //一个项目应该只存在一个main方法
    4. public class Application {
    5. public static void main(String[] args) {
    6. //一个对象的实际类型是确定的
    7. //new Student();
    8. //new Person();
    9. //可以指向的引用类型就不确定了:父类的引用指向子类
    10. //Student子类能调用的方法都是自己的或者继承父类的
    11. Student S1 = new Student();
    12. //Person父类,可以指向子类,但是不能调用子类独有的方法
    13. Person S2 = new Student();
    14. Object S3 = new Student();
    15. //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
    16. S2.run(); //子类重写了父类的方法,就执行子类的方法
    17. S1.run();
    18. //想调用的话,用强制转换
    19. ((Student) S2).eat();
    20. }
    21. }

    instanceof(类型转换)

     instanceof关键字:判断一个对象是什么类型

    创建一个父类Person,子类Student、Teacher

    代码演示:

    1. public static void main(String[] args) {
    2. //Object > Person > Student
    3. Object object = new Student();
    4. System.out.println(object instanceof Student); //true
    5. System.out.println(object instanceof Person); //true
    6. System.out.println(object instanceof Object); //true
    7. System.out.println(object instanceof Teacher); //false
    8. System.out.println(object instanceof String); //false
    9. System.out.println("============================");
    10. Person person = new Student();
    11. System.out.println(person instanceof Student); //true
    12. System.out.println(person instanceof Person); //true
    13. System.out.println(person instanceof Object); //true
    14. System.out.println(person instanceof Teacher); //false
    15. //System.out.println(person instanceof String); 编译报错
    16. System.out.println("============================");
    17. Student student = new Student();
    18. System.out.println(student instanceof Student); //true
    19. System.out.println(student instanceof Person); //true
    20. System.out.println(student instanceof Object); //true
    21. //System.out.println(student instanceof Teacher); 编译报错
    22. //System.out.println(student instanceof String); 编译报错
    23. }

    类型转换:

      1. 父类引用指向子类的对象;

      2. 把子类转换为父类,向上转型;可能丢失一些方法;

      3. 把父类转换为子类,向下转型;强制转换;

      4. 方便方法的调用,减少重复的代码。简洁

    代码演示:(父类、子类同上)

    1. //类型之间的转换:父 子
    2. //高 低
    3. Person obj = new Student();
    4. //将上面的对象转换为Student类型,就可以使用Student的方法了
    5. Student student = (Student) obj;
    6. student.go();
    7. //或
    8. ((Student) obj).go();
    9. //低 高
    10. //子类转换为父类,可能会丢失自己的一些方法
    11. Student student1 = new Student();
    12. student1.go();
    13. Person person = student1;

  • 相关阅读:
    【比较mybatis、lazy、sqltoy、lambda、操作数据 】操作批量新增、分页查询【一】
    基于VC++的WEB浏览器的实现
    ChatGPT prompt汇总-个人使用-持续更新....
    Python基于OpenCV的人脸识别自助商店(源码&部署视频)
    1475_AURIX TC275 WDT的寄存器概览
    搭建算法接口及访问——Nginx、fastapi、postman配置使用
    QSS编辑器QssEditor之三:Ribbon控件与换肤
    力扣刷题Days3第二题(js)
    【深度学习】记录为什么没有调用GPU
    Mindspore 控制流中存在原地更新操作类副作用算子时循环值不更新问题记录
  • 原文地址:https://blog.csdn.net/chenyang_88/article/details/138868493