您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页instance of

instance of

来源:五一七教育网

(其中Person是父类,Student和Teacher均继承Person)
package oop;

import oop.demo06.Person;
import oop.demo06.Student;
import oop.demo06.Teacher;

public class Application {
public static void main(String[] args) {
/*
Object > String
Object > Person > Teacher
Object > Person > Student
*/
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();
//把obj转换为student,即可调用student的方法(假设student有一个go方法
((Studnet) obj).go
//把子类转换为父类,称为向上转型,可能丢失方法
Student student = new Studen();
student.go();
Person person = student

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务