首页 > 编程语言 > 详细

java面对对象编程(3)instanceof static 代码块 抽象类 接口 内部类(未写完)

时间:2021-03-01 09:24:58      阅读:76      评论:0      收藏:0      [点我收藏+]

instanceof

判断一个对象是什么类型

Object > String
        //Object> Person > Teacher
        //Object> Person > Student
        Object object = new Student();

        //System.out.println(X instanceof Y);//x,y是否有父子关系,决定编译是否报错

        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

        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);// 编译报错

        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);//
//类型之间的转换 : 基本类型的转换  高低  64  32  16  高转低要强转
        //父类  子类


       // Student student = new Student();
        //高                    低(低转高很轻松)
        Person obj = new Student();
        //Student类有go方法,Person类中没有go方法   obj.go();报错

        //obj 将这个对象转换为Student类型,我们就可以使用Student类型的方法了(当前为Person类型)

        //(Student)obj; +Ctrl +Alt +v
        Student student = (Student) obj;
        student.go();
        //两句话可以写成一句话
        ((Student)obj).go();

        //低转高
        //子类转换为父类,可能丢失自己本来的方法
        Student student1 = new Student();
        student1.go();
        Person person = student1;
        //person.go();  无法直接调用

/*
1.父类引用指向子类的对象   Person person = student1;
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型,强制转换
4.方便方法的调用,减少重复的代码,简洁

抽象: 封转 继承  多态          抽象类  接口
 */

static

技术分享图片

代码块

技术分享图片

补充

技术分享图片

java面对对象编程(3)instanceof static 代码块 抽象类 接口 内部类(未写完)

原文:https://www.cnblogs.com/tqdm/p/14461412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!