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只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...
随机推荐
- SpringDataJPA+QueryDSL玩转态动条件/投影查询
在本文之前,本应当专门有一篇博客讲解SpringDataJPA使用自带的Specification+JpaSpecificationExecutor去说明如何玩条件查询,但是看到新奇.编码更简单易懂的 ...
- Git用在公司项目上的操作
修改代码后再次提交 搭档优化好他自己的代码后,我想在vscode上看看他优化后的结果 此时直接git pull origin就可以看到了 j接下来的一些指令,慢慢了解... 分支本身已经在我上面 以下 ...
- 2018-6-29-PTA-6-2-多项式求值
title author date CreateTime categories PTA 6-2 多项式求值 lindexi 2018-06-29 15:24:28 +0800 2018-6-14 22 ...
- Android Simulator Shortcut keys
按钮 快捷键 Back Ctrl+Backspace Battery Ctrl+Shift+B Cellular Ctrl+Shift+C D-pad Ctrl+Shift+D Enter zoom ...
- Vue学习笔记-目录结构
1.采用脚手架构建的项目基本目录结构 可能会有些许差别,但是大致基本目录都差不多 2.项目入口(index.html,main.js,App.vue) 一般情况下,我们都习惯性将 index.html ...
- 一个APP从启动到主页面显示经历了哪些过程?
①点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求: ②system_server进程接收到请求后,向zygote进程 ...
- 定位、识别;目标检测,FasterRCNN
定位: 针对分类利用softmax损失函数,针对定位利用L2损失函数(或L1.回归损失等) 人关节点检测 针对连续变量和离散变量需要采用不同种类的损失函数. 识别: 解决方案: 1.利用滑动窗口,框的 ...
- Spring Boot + Docker + K8S 简单示例
前言 最近看了看k8s,感觉用这个管理docker确实比自己写一坨脚本进步太多了,简直不是一个次原的东西. 看着k8s的官方文档随手写了个小Demo,一个基于k8s的spring boot服务. 代码 ...
- 源码分析 Kafka 消息发送流程(文末附流程图)
温馨提示:本文基于 Kafka 2.2.1 版本.本文主要是以源码的手段一步一步探究消息发送流程,如果对源码不感兴趣,可以直接跳到文末查看消息发送流程图与消息发送本地缓存存储结构. 从上文 初识 Ka ...
- LibreOJ 6277. 数列分块入门 1 题解
题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 \( ...