前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java的学习笔记(12)对象 七

Java的学习笔记(12)对象 七

作者头像
卢衍飞
发布2023-02-13 21:29:31
4480
发布2023-02-13 21:29:31
举报
文章被收录于专栏:技术分享交流技术分享交流

对象13.1:instanceof instanceof(类型转换)引用类型,判断一个对象是什么类型

package oop.Demo07;

public class Application {

代码语言:javascript
复制
public static void main(String[] args) {
  //  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);     //ture
    System.out.println(object instanceof Person);       //ture
    System.out.println(object instanceof Object);       //ture
    System.out.println(object instanceof Teacher);      //false    此处的object是Student类型的,与Teacher不是父子关系
    System.out.println(object instanceof String);       //false

    Person person = new Student();
    System.out.println(person instanceof Student);     //ture
    System.out.println(person instanceof Person);       //ture
    System.out.println(person instanceof Object);       //ture
    System.out.println(person instanceof Teacher);      //false
//    System.out.println(person instanceof String);       //编译报错,没有关系就报错

    Student student = new Student();
    System.out.println(student instanceof Student);     //ture
    System.out.println(student instanceof Person);       //ture
    System.out.println(student instanceof Object);       //ture
  //  System.out.println(student instanceof Teacher);      //编译报错
    //    System.out.println(student instanceof String);       //编译报错

}

} package oop.Demo07;

public class Person {

代码语言:javascript
复制
public void run(){
    System.out.println("run");
}

} package oop.Demo07;

public class Student extends Person{

代码语言:javascript
复制
public void go() {
    System.out.println("go");
}

} package oop.Demo07;

public class Teacher extends Person{ } 对象13.2 类型之间的转换 // 父类 public class Parent {}

// 子类 public class Son extends Parent {}

// 女儿类 public class Daughter extends Parent {} 子类转父类 Son son = new Son(); Parent parent = (Parent) son; 运行没问题

父类的引用指向子类转父类对象 Parent son = new Son(); Parent parent = (Parent) son; 运行没问题,这种肯定也是可以的

父类转子类

父类的引用指向父类转子类对象 Parent parent = new Parent(); Son son = (Son) parent; 运行有问题会报 ClassCastException

父类的引用指向子类转子类对象 Parent parent = new Son(); Son son = (Son) parent; 运行没有问题

父类的引用指向子类转另一个子类对象 Parent parent = new Son(); Daughter daughter = (Daughter) parent; 运行时报出 ClassCastException 的异常

package oop.Demo07;

public class Application {

代码语言:javascript
复制
public static void main(String[] args) {

    //  Person父类  run()方法   ,   Student子类 go()方法
    // 父类的方法,子类可以用,但是子类特有的方法,父类不可以调用
    Person person = new Person();
    person.run();
    //  person.go();   子类特有的方法,父类不可以调用
    Student student1 = new Student();
    student1.go();
    student1.run();

    //向上转型,子转父
    //也可以理解为将一个子类的对象转化为父类的对象(隐形转型,自动的),小类转大类
    Person one = new Person();
    one.run();

    Person two = new Student();
    //  父类引用子类实例,可以调用子类重写父类的的方法以及父类派生的方法,无法调用子类独有的方法
    //  注意:父类中的静态方法无法被子类重写,所以向上转型之后,只能调用到父类原有的静态方法。(如果父类中存在静态方法)
    two.run();
    // two.go();     报错,父类引用子类实例,无法调用子类独有的方法
    //子类转化为父类,可能会丢失自己本来的一些方法。  子类转父类,子类拓展的方法会丢失。

    // 向下转型,必须强转
    //类型之间的的转化 :父转子,向下转换要强制转换,也就是父类转为子类要强转
     Person obj = new Student();
    // obj.go()   会报错, obj是父类Person对象实例,需要将obj转换为Student类型,才可以使用Student类型的方法
     Student student = (Student) obj;
     student.go();
    //二合一:  ((Student) obj).go();

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

  */

}

}

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023 年 01 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com