小白学java第三十日:面向对象的类型转换!

instanceof与类型的转换

  • 判断一个对象是什么类型

  • instanceof通常用来判断两个类或方法之间是否有联系

package com.yuecheng.oop;

import com.yuecheng.oop.Demo06.Student;
import com.yuecheng.oop.Demo06.Person;
import com.yuecheng.oop.Demo06.Teacher;

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

        //instanceof 左右类或者对象间有练习就会返回true;否则返回flase

        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 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
        // 这里Person是和Teacher有关系的,但是通过Student new出的person对象和Teacher无关的
        //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 String);编译就会报错
        //System.out.println(student instanceof Teacher);编译就会报错,
        //因为这里的学生和老师并没有直接的联系,只是都继承了Person这个父类罢了。


    }


}

类型的转换

  • 父—>子(高转低,需要强制转换)
    • 因为 父类想要使用子类独有的方法,所以为了实现这个功能,就把父类伪装成子类即可。
package com.yuecheng.oop;

import com.yuecheng.oop.Demo06.Student;
import com.yuecheng.oop.Demo06.Person;
import com.yuecheng.oop.Demo06.Teacher;

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

        Student student = new Student();

        //student.go();

        Person S1 = new Student();
        //S1.go();会报错,不能使用,因为高类不能调用低类的方法,所以要进行类型的转换

        //((Student)S1).go();//对S1进行强制转换为Student类型,可以使用GO

        //或者
        Student s1 = (Student) S1;

        s1.go();//转换完成 可以使用


    }




}

  • 子---->父(低转高自动转化)
package com.yuecheng.oop.Demo06;

public class Teacher extends Person{
    //类型的转换:子   --  父
    public static void main(String[] args) {

        Student student = new Student();

        Person s2 = student;//自动转换

        //s2.go();  子类转化为父类,丢失子类的go()方法!
        //所以要注意,子类转父类可能会丢失一些方法!
        

    }



}

注意

  1. 父类引用可以指向子类对象
  2. 把子类转为父类,向上转型,直接转,但会丢失子类一些独有的方法,
  3. 父转子:向下转型,需要强制转型,丢失父类被子类所重写掉的方法!
  4. 方便方法的调用,减少重复代码!
  5. 抽象:封装!继承!多态!
  6. 抽象的编程思想:需要持续学习:茅塞顿开!多实践!多测试大脑中的想法
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玥骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值