Title

instanceof类型判断与转换

instanceof类型判断

=========================================================================

代码演示---父类Person

package com.tea.Demo06;

public class Person {

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

代码演示---子类Student

package com.tea.Demo06;

public class Student extends Person{

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

代码演示---子类Teacher

package com.tea.Demo06;

public class Teacher extends Person{

}

代码演示---测试类

package com.tea.Demo06;

import sun.rmi.runtime.NewThreadAction;

public class Application {
    public static void main(String[] args) {

        //Object>Person>Student
        //Object>Person>student
        //Object>Person>student
        //Object>Person>Teacher
        //Object>String
        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>Student
        //Person>Student
        //Object>Person>Student
        //Object>Person>Teacher
        //object>Person,object>String
        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("================================");
        //Object>Person>Student
        //Object>Person>Student
        //Object>Person>Student
        //Object>Person>Student,Object>Person>Teacher
        //Object>Person>Student,object>String
        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); //编译报错

        //以上例子公式:System.out.println(X instanceof y);
        //公式是否能编译通过决定于x与y之间的关系(我理解是线性关系通过,树形则报错)
    }
}

=========================================================================

父类与子类对象的类型转换

父类与子类方法不变,若由父类实例化的具体对象要调用子类中特有的方法,则需要将这个对象的类型进行强制转换。若高------->低,则需要强制转换

========================================================================

代码演示---测试类(高------->低)

package com.tea.Demo06;

import sun.rmi.runtime.NewThreadAction;

public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父 子

        //高<--------------------低
        Person student1 = new Student();

        //若由父类实例化的具体对象要调用子类中特有的方法,则需要将这个对象的类型进行强制转换
        // 将student类型(此刻为Person类型)强制转换为Student类型
        // 即高------->低,需要强制转换
        ((Student)student1).go();

    }
}

=========================================================================

代码演示---测试类(高------->低)

package com.tea.Demo06;

import sun.rmi.runtime.NewThreadAction;

public class Application {
    public static void main(String[] args) {
        //类型之间的转换:父 子

        //子类转换为父类可能会丢失自己特有的方法
        Student student = new Student();
        student.go();
        Person Person = student;
    }
}

=========================================================================

总结

  1. 父类引用执行子类对象

  2. 把子类转换为父类,向上转型(子类转换为父类可能会丢失自己特有的方法)

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

  4. 方便方法的调用

    抽象:封装,继承,多态

=========================================================================

posted @ 2022-10-01 20:15  花里胡哨的渣渣  阅读(23)  评论(0)    收藏  举报