43-instanceof 和 类型转换







instanceof 和 类型转换

判断一个对象是什么类型。判断两个类中是否存在父子关系。


示例

main 方法

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        //System.out.println(X instanceof Y);  //首先判断 X 和 Y 之间是否存在父子关系,如果不存在,则编译报错。如果存在,则编译通过。
        // 如果 X 指向的类型和 Y 存在父子关系,则返回 true
        Object object = new Student_01();
        System.out.println(object instanceof Student_01);  //true
        System.out.println(object instanceof Person_01);  //true
        System.out.println(object instanceof Object);  //true
        System.out.println(object instanceof Teacher_01);  //false
        System.out.println(object instanceof String);  //false

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

        Person_01 person_01 = new Student_01();
        System.out.println(person_01 instanceof Student_01);  //true
        System.out.println(person_01 instanceof Person_01);  //true
        System.out.println(person_01 instanceof Object);  //true
        System.out.println(person_01 instanceof Teacher_01);  //false
        //System.out.println(person_01 instanceof String);  //编译错误

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

        Student_01 student = new Student_01();
        System.out.println(student instanceof Student_01);  //true
        System.out.println(student instanceof Person_01);  //true
        System.out.println(student instanceof Object);  //true
        //System.out.println(student instanceof Teacher_01);  //编译错误
        //System.out.println(student instanceof String);  //编译错误
    }
}

父类 Person

public class Person_01 {
}

子类 Student

public class Student_01 extends Person_01 {
}

子类 Teacher

public class Teacher_01 extends Person_01 {
}

类型转换


高转低

main 方法

public class Application {
    public static void main(String[] args) {
        //类型之间的转换
        //基本类型转换 从高到低 父 子
        //高                        低
        Person_01 student = new Student_01();
        //student 将这个对象转换为 Student 类型,就可以使用 Student 类型的方法
        //student.go();  //编译会报错,因为 Person 中没有 go 方法
        Student_01 student_02 = (Student_01) student;  //将 student 强制转换成 Student_01
        student_02.go();
        //或者
        ((Student_01) student).go();
    }
}

父类 Person

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

子类 Student

public class Student_01 extends Person_01 {
    public void go() {
        System.out.println("go");
    }
}

低转高

main 方法

public class Application {
    public static void main(String[] args) {
        Student_01 student_03 = new Student_01();
        student_03.go();
        Person_01 person_02 = student_03;  //因为 student 比 person 低,所以低转高可以直接转
        //person_02.go();  //会编译失败,因为如果子类转换为父类,可能会丢失一些自己本来的方法
    }
}

父类 Person

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

子类 Student

public class Student_01 extends Person_01 {
    public void go() {
        System.out.println("go");
    }
}

注意

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

  2. 把子类转换成父类,向上转型

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

  4. 方便方法的调用


暂无评论

发送评论 编辑评论


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