首页 > 其他分享 >instanceof与类型转换

instanceof与类型转换

时间:2023-02-27 23:57:10浏览次数:31  
标签:instanceof 类型转换 System Person Student println out

instanceof与类型转换

package com.andy.base.oop.demo01.demo06;

public class Teacher extends Person {


}

package com.andy.base.oop.demo01.demo06;

public class Student extends Person {

    public  void go(){
        System.out.println("go");
    }

}

/*
   //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(person instanceof String);// 编译错误
 */

package com.andy.base.oop.demo01.demo06;

public class Person {

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

}


main

package com.andy.base.oop.demo01;


import com.andy.base.oop.demo01.demo06.Person;
import com.andy.base.oop.demo01.demo06.Student;

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

        //基本类型转换: 父(高)   子(低)

        //高                    低
        Person student = new Student();
       // student.go();    //student调用的是 Person ,Person类中没有go方法

        //student 将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
        ((Student)student).go();

        Student student1 = new Student();
        student1.go();
        Person person = student;    //低 --->高 (自动)Student --->Person


    }
}


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

封装、继承 、多态    抽象类,接口
 */

标签:instanceof,类型转换,System,Person,Student,println,out
From: https://www.cnblogs.com/zhongjianYuan/p/17162412.html

相关文章

  • 类型转换
    类型转换由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换低----------------------------------------------------------->高byte,short,char-->int-......
  • 将后端的application/json的格式数据类型转换成前端需要的类型格式
    前提:后端返回的数据内容但是红框的数据对于前端来说是不正确的数据所以我感觉前端处理这个数据本身这个操作都很傻X但是我尝试进行转换代码如下:得到的数据:点击查......
  • 类型转换
    隐式转换:1、当小的数据类型和大的数据类型在一起运算的时候,小的数据类型会提升为大的数据类型再进行运算。特别注意的是:byte、short、char在进行运算的时候,都会直接提......
  • 类型转换(续)
    JDK7新特性,数字之间可以用下划线分割在操作数值比较大的数时应注意内存溢出问题https://space.bilibili.com/1930936051?spm_id_from=333.1007.0.0......
  • 02_18_Java语音进阶||day18_Java基础小节练习(17-20部分)数据类型转换&运算符&方法入门
    第一部分数据类型转换&运算符&方法入门第一题编写步骤:定义类Test1定义main方法定义两个byte类型变量b1,b2,并分别赋值为10和20.定义变量b3,保存b1和b2的和,并输出.定义两......
  • 59.类的自动转换和强制类型转换
    程序清单11.16stonewt.h#pragmaonce//stone.h--Stonewt类声明#ifndefSTONEWT_H_#defineSTONEWT_H_classStonewt{private: enum{Lbs_per_stn=14};//poun......
  • C# 自定义枚举类型转换
    //stringtoenumEAccountRolevar=(EAccountRole)Enum.Parse(typeof(EAccountRole),RoleManage.Instance.CurrentAccount.Role); //enumtolistpublicIEnumer......
  • 类型转换
    由于java是强类型语言,所有要进行运算的时候,需要用到类型转换低------------------------------------------->高byte,short,char--->int--->long--->float--->double ......
  • JavaScript 强制类型转换
    <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <scripttype="text/javascript"> /* *强制类型转换 * -指将一个数据......
  • SpringBoot19 - 数据类型转换
    数据类型转换​ 先把问题描述一下,这位开发者连接数据库正常操作,但是运行程序后显示的信息是密码错误。java.sql.SQLException:Accessdeniedforuser'root'@'localho......