Java基础:多态、instanceof、类型转换

多态

动态编译,类型,可扩展性

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

  • 一个对象的实际类型是确定的,但可指向对象的引用类型是多样的(有继承关系的类)

  • 注意事项:

    • 多态是方法的多态,属性没有多态
    • 存在继承关系的类才能进行转换,可以互相转换(类型转换异常:ClassCastException)
    • 存在条件
      • 继承关系
      • 方法重写
      • 父类引用指向子类对象 Father f = new Son();
  • 不能重写的方法

    • static 静态方法,属于类,不属于实例
    • final 常量,无法修改
    • private 私有方法
  1. 调用父类方法
//父类
public class Person {
    public void run(){
        System.out.println("run");
    }
}

//子类
public class Student extends Person{}

//Application
public class Application {
    public static void main(String[] args) {
        //对象的实际类型确定
        //new Student();
        //new Person();

        //可指向的引用类型不确定:父类的引用指向子类的类型(需要有继承关系)
        Student s1 = new Student();
        Person s2 = new Student();
        Object s3 = new Student();

        s2.run();//run,使用父类中的方法
    }
}

s2由父类的引用指向子类的类型,在子类没有对方法重写时,调用父类中的方法

  1. 子类对其进行重写
//父类
public class Person {
    public void run(){
        System.out.println("run");
    }
}

//子类
public class Student extends Person{
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}

//Application
public class Application {
    public static void main(String[] args) {
        //可指向的引用类型不确定:父类的引用指向子类的类型(需要有继承关系)
        //Student能调用的方法是自己或继承父类的
        Student s1 = new Student();
        //Person父类可以指向子类,但不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        s2.run();//son,子类重写了父类的方法,执行子类的方法
        s1.run();//son

        //对象能指向哪些方法,看对象左边的类型
        //s2.eat();//无法调用子类中的方法,除非对其进行强制类型转换,具有继承关系可以互相转换
    }
}

子类重写了父类的方法,执行子类的方法,方法是使用取决于引用的类型

instanceof

insteaceof :类型判断,判断对象是否和类型有继承关系

  • x instanceof Y 能否编译通过取决于x引用的类型与Y之间是否存在继承关系

  • instanceof的结果取决于x指向的类型类型Y之间是否存在继承关系

public static void main(String[] args) {
    //Object>String
    //Object>Person>Teacher
    //Object>Person>Student

    //X instanceof Y 能否编译通过取决于X与Y之间是否存在继承关系

    Object object = new Student();
    System.out.println(object instanceof Student);//true
    System.out.println(object instanceof Person);//true
    System.out.println(object instanceof Object);//true
    System.out.println(object instanceof Teacher);//false
    System.out.println(object instanceof String);//false

    System.out.println("=======================================");

    Person person = new Student();
    System.out.println(person instanceof Student);//true
    System.out.println(person instanceof Person);//true
    System.out.println(person instanceof Object);//true
    System.out.println(person instanceof Teacher);//false
    //System.out.println(person instanceof String);//编译报错

    System.out.println("=======================================");

    Student student = new Student();
    System.out.println(student instanceof Student);//true
    System.out.println(student instanceof Person);//true
    System.out.println(student instanceof Object);//true
    //System.out.println(student instanceof Teacher);//编译报错
    //System.out.println(student instanceof String);//编译报错
}

类型转换

类型转换,高->低:强制转换

//类型间的转换:高转低强行转换,低转高较为容易(父>子)

//高                 低
Person obj = new Student();

//将student转换为Student类型,就可以使用Student类型的方法
Student student = (Student) obj;
student.go();
//or
((Student)obj).go();

类型转换,低->高:向上转型

//子类转换为父类可能会丢失一些本来的方法
Student student = new Student();
student.go();
Person person = student;
//person.go();//无法使用

总结:

  • 父类引用指向子类的对象
  • 把子类转换为父类,向上转型,可能丢失方法
  • 把父类转换为子类,向下转型,强制转换,可能丢失精度
  • 方便方法的调用,减少重复的代码,简洁
posted @   chachan53  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示