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

instanceof和类型转换

时间:2023-05-20 15:35:18浏览次数:37  
标签:instanceof 类型转换 System Student println public out

instanceof和类型转换

instanceof

package oop.demo01.demo06;

public class Person {

}
package oop.demo01.demo06;

public class Teacher {
}
package oop.demo01.demo06;

public class Student extends Person {

}
package oop.demo01.demo06;

public class Application {
    public static void main(String[] args) {
        //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
        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);//编译报错
        //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);//编译报错
    }
}

类型转换

package oop.demo01.demo06;

public class Person {
public void run(){
    System.out.println("run");
}
}
package oop.demo01.demo06;

public class Teacher {
}
package oop.demo01.demo06;

public class Student extends Person {
public void go(){
    System.out.println("go");
}
}
package oop.demo01.demo06;

public class Application {
    public static void main(String[] args) {
       //类型之间的转换:父    子
        Person obj=new Student();
        //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
        ((Student) obj) .go();//类型转换
        //子类转换为父类,可能丢失自己本来的方法
    }
}

小结

  1. 父类引用指向子类对象。

  2. 把子类对象转换为父类,向上转型。

  3. 把父类转换成子类,向下转型,强制转换。

  4. 方便方法的调用,减少重复的代码!

标签:instanceof,类型转换,System,Student,println,public,out
From: https://www.cnblogs.com/sx-xiaoL/p/17417276.html

相关文章

  • 类型转换
    类型转换由于java是强制类型语言,所以要进行有些运算的时候,需要用到类型转换。byte,short,char-->int-->long-->float-->double低--------------------------------------------------->高运算中,不同类型的数据先转化为同一类型,然后再进行运算。强制类型转换高-->低紫......
  • mybatis自定义类型转换器
    Mybatis类型转换介绍[url]http://haohaoxuexi.iteye.com/blog/1847854[/url]mybatis提供了对自定义的类型转换器(typeHandler)的支持,因此我们可以自己编写类型转换器来实现这一自动转换的功能。[b][color=red]注意:1.使用的时候,resultMap也select的SQL......
  • 05 强制类型转换
    05强制类型转换作者:FL博客:https://www.cnblogs.com/flblogs/String将其他数据类型转换为String方式一:调用数据类型的toString()方法toString方法不会影响原变量,而是将转换的结果返回注意:toString方法不能转换null和unidentifiedvara=123;a=a.toString();......
  • 记录一件很神奇的类型转换问题(springboot项目+echarts)
    今天博主在应付学校的实验,想要使用echarts绘制一张很简单的条形图(博主是初学者),如下(时间还未作排序) 对于横轴,我封装了一个dateList,这个datelist是用java,将数据库中date类型的数据,提取其年月拼装而成的,代码如下:Stringdate=String.valueOf(art.getArticleCreateTime().getYea......
  • 我刚才用了dynamic_cast 你给我普及一下C++ 中这几种类型转换吧
    我刚才用了dynamic_cast你给我普及一下C++中这几种类型转换吧在C++中,有几种类型转换的方式,包括:隐式转换在一些情况下,编译器会自动进行类型转换。比如将整型变量赋值给浮点型变量,编译器就会自动将整型变量转换为浮点型变量。但是在大多数情况下,使用隐式转换可能会引起一些问......
  • 类型转换: list string
     /*一:str->list二:list->str1list元素全为str2list元素不全为str*/  一:str->listif__name__=='__main__':str1="12345"print(list(str1))#['1','2','3','......
  • (隐式)自动类型转换
    #include<stdio.h>intmain(){chara[6]="Hello";longl=1;printf("sizeof(*a+0)=%dsizeof(*a+l)=%d\n",sizeof(*a+0),sizeof(*a+l));printf("(*a+0)=%c(*a+l)=%c\n",(*a+0),(*a+l));}ViewCode 自动类......
  • 自动类型转换
    所以说,只要找到第一个计算的表达式,将其乘1ll,整个式子就是longlong的了。还有就是有符号和无符号乘法的时候一定要注意:int(-1)*(unsignedint)(10000)将无法得到-10000!(除非是unsignedshort)......
  • TypeScript 学习笔记 — 数组常见的类型转换操作记录(十四)
    获取长度lengthtypeLengthOfTuple<Textendsany[]>=T["length"];typeA=LengthOfTuple<["B","F","E"]>;//3typeB=LengthOfTuple<[]>;//0取第一项FirstItemtypeFirstItem<Textendsany[]>......
  • JavaScript的类型转换(字符转数字,数字转字符)
    在Java中,基本类型之间的强制转换也不是这样的,比如,整数要转换成字符串,必须使用Integer.toString()静态方法或者String.valueOf()静态方法,把字符串转换为整数,必须使用Integer.valueOf()。可见,不能把JavaScript中的类型转换看作为“强制类型转换”。在JavaSc......