12.instanceof和类型转换
package com.oop.demo07;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.demo07;
public class Student extends Person {
public void go() {
System.out.println("go");
}
}
package com.oop.demo07;
public class Teacher extends Person{
}
package com.oop; import com.oop.demo07.Person;
import com.oop.demo07.Student;
import com.oop.demo07.Teacher;
public class Application { 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);//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(student instanceof String);//编译报错 }
}
package com.oop; import com.oop.demo07.Person;
import com.oop.demo07.Student; public class Application { public static void main(String[] args) {
//类型之间的转换:基本类型转换 父 子 //高 低
Person obj = new Student(); //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
//Student student = (Student) obj;
//student.go();
((Student) obj).go(); //子类转换成父类会丢失一些独有的方法!
Student student = new Student();
student.go();
Person person = student; }
}
12.instanceof和类型转换的更多相关文章
- 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
12.数据类型转换.格式化.校验 12.1.数据绑定流程 Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...
- instanceof 和类型转换
instanceof 和类型转换 instanceof 判断a 和 B 类型是否相似 公式 System.out.println(a instanceof B); //true / false 编译是 ...
- Java面向对象系列(11)- instanceof和类型转换
instanceof 先看引用类型的类和instanceof比较的类有没有父子关系,有则可以编译,IDEA不报错 new一个对象,对象new所在的类和instanceof比较的类有没有父子关系,有则为 ...
- instanceof和类型转换
什么是instanceof 判断一个对象是什么类型 注意点 X 和 Y 必须要有父子关系 否则编译都会失败 X对象只要是Y的子类(无论 是 儿子 还是 孙子 还是 曾孙....)X instanceo ...
- 多态(instanceof)
多态调用的三种格式 * A:多态的定义格式: * 就是父类的引用变量指向子类对象 父类类型 变量名 = new 子类类型(); 变量名.方法名(); * B: 普通类多态定义的格式 父类 变量名 = ...
- SQL类型转换以及自动在前面补0满足10位工号标示法
1,自动在前面补0满足10位工号标示法 SELECT rtrim(ltrim(right(cast('00000000'+rtrim(CAST(数值 as int)) as varchar(20)), ...
- Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数
由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实 ...
- C++中的新型类型转换
1,C 语言中已经有类型之间的强制转换,C++ 做了改善: 2,C 方式的强制类型转换: 1,(Type) (Expression): 2,Type (Expression): 1,这种方式和上述方式 ...
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]
类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...
随机推荐
- linux 创建你的 /proc 文件
一旦你有一个定义好的 read_proc 函数, 你应当连接它到 /proc 层次中的一个入口项. 使用一个 creat_proc_read_entry 调用: struct proc_dir_ent ...
- java编译器优化和运行期优化
概述 最近在看jvm优化,总结一下学习的相关知识 (一)javac编译器 编译过程 1.解析与填充符号表过程 1).词法.语法分析 词法分析将源代码的字符流转变为标记集合,单个字符是程序编 ...
- springboot+jpa分页(Pageable+Page)
Pageable+Page实现分页无需配置,也不需要加入jar包(maven依赖) Controller控制层 package com.gxuwz.late.controller; import co ...
- 列表内容自动向上滚动(原生JS)
效果展示 (鼠标移入,滚动停止:鼠标移出,滚动继续) 实现原理 1. html结构:核心是ul > li,ul外层包裹着div.因为想要内容循环滚动无缝衔接,所以在原有ul后面还要有一个一样内容 ...
- CKEditor配置,最适合新手两种方式详解。
CKEditor.js的配置,大概有两种方式,这里有基础版和全面的版本可以试验 https://cdn.ckeditor.com/4.8.0/full-all/ckeditor.js http://c ...
- C# 强转空会不会出现异常
有小伙伴问我强转 null 会不会出现异常,我告诉他,如果是引用类型那么不会,如果是值类型,那么会出现空异常 如果是引用类型,只要是空类型,是支持随意转换,如下面代码,这是可以运行 class Pro ...
- Java 学习笔记(14)—— 文件操作
java文件操作主要封装在Java.io.File中,而文件读写一般采用的是流的方式,Java流封装在 java.io 包中.Java中流可以理解为一个有序的字符序列,从一端导向到另一端.建立了一个流 ...
- Ceph 之RGW Pub-Sub Module
Overview Pub-Sub module 顾名思义是一个发布订阅相关的模块.Pub-Sub module 为对象存储的变更事件提供一种发布-订阅机制.而发布-订阅架构本身应用非常广泛,如公有云G ...
- JAVA8之 Stream 流(四)
如果说前面几章是函数式编程的方法论,那么 Stream 流就应该是 JAVA8 为我们提供的最佳实践. Stream 流的定义 Stream 是支持串行和并行操作的一系列元素.流操作会被组合到流管道中 ...
- C# list与table的互转
//list转化为table public static DataTable ListToDataTable<T>(List<T> entitys) { //检查实体集合不能为 ...