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只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...
随机推荐
- H3C DHCP服务器显示及维护
- JS只执行一次
1.闭包实现. <script> window.onload = function () { function once(fn) { var result; return function ...
- webpack+babel+react+antd技术栈的基础配置
webpack+babel+react+antd技术栈的基础配置 前段时间使用webpack+babel+react+antd做了一套后台管理系统,刚开始被一大堆的新知识压的喘不过气来,压力挺大的.还 ...
- scrf 原理及flask-wtf防护
了解什么是scrf? SCRF跨站点请求伪造Cross—Site Request Forgery) 指恶意用户通过个人用户的点击,然而盗用用户的账号信息,并发送邮件.虚拟货币的转账,以及一些重要的事务 ...
- ajax异步发送时遇到的问题
问题原因是:controller中方法名与url中的名字不一样造成的 解决办法:找到错误的方法名,将其与url中的方法名统一:
- mysql主从之binlog日志
mysql的binlog说明 主从复制依赖于二进制日志文件,简称为binlog binlog里面有存放了偏移信息 mysql主库binlog信息查看命令 [root@master ~]# mysql ...
- 从零开始のcocos2dx生活(三)Scheduler
文章目录 取模 Timer() 变量 设置定时器Timer() 一些成员函数 Scheduler() 变量 初始化 哈希表 构造函数schedule() 开启定时器Update() 析构函数~Upda ...
- leetcode.310最小高度树
对于一个具有树特征的无向图,我们可选择任何一个节点作为根.图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树.给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点. ...
- 2020了你还不会Java8新特性?(五)收集器比较器用法详解及源码剖析
收集器用法详解与多级分组和分区 为什么在collectors类中定义一个静态内部类? static class CollectorImpl<T, A, R> implements Coll ...
- Google 开源的 Python 命令行库:深入 fire(二)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...