instanceof与类型转换
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与类型转换的更多相关文章
- instanceof 和类型转换
instanceof 和类型转换 instanceof 判断a 和 B 类型是否相似 公式 System.out.println(a instanceof B); //true / false 编译是 ...
- 12.instanceof和类型转换
Instanceof: 判断一个对象是什么类型的~,可以判断两个类之间是否存在父子关系 package com.oop.demo07; public class Person { public voi ...
- Java面向对象系列(11)- instanceof和类型转换
instanceof 先看引用类型的类和instanceof比较的类有没有父子关系,有则可以编译,IDEA不报错 new一个对象,对象new所在的类和instanceof比较的类有没有父子关系,有则为 ...
- instanceof和类型转换
什么是instanceof 判断一个对象是什么类型 注意点 X 和 Y 必须要有父子关系 否则编译都会失败 X对象只要是Y的子类(无论 是 儿子 还是 孙子 还是 曾孙....)X instanceo ...
- Java匹马行天下之JavaSE核心技术——面向对象
面向对象 注: 看此篇时强烈建议有一定的面向对象思想基础,有一定的基础后先翻到下面看第九条: 9.面向对象: 从未封装→封装→继承→多态→抽象类→接口的代码演变 按这个逻辑去看,,哪有不理解 ...
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]
类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...
- JavaSE(二)之继承、封装、多态
学习完类与对象终于认识到什么是类,什么是对象了.接下来要看的就是java的三大特征:继承.封装.多态. 一.封装(数据的隐藏) 在定义一个对象的特性的时候,有必要决定这些特性的可见性,即哪些特性对外部 ...
- 160426、JavaScript 秘密花园
简介 关于作者 这篇文章的作者是两位 Stack Overflow 用户, 伊沃·韦特泽尔 Ivo Wetzel(写作) 和 张易江 Zhang Yi Jiang(设计). 贡献者 贡献者 中文翻译 ...
- java基础语法4--封装,继承,多态
学习路线: 未封装==>封装==>继承==>多态==>抽象类 首先还是那句话,万物皆对象,对象有行为和属性. 一:封装 1.封装的概念: 信息隐蔽和对象的属性及操作结合成一个独 ...
- JAVA基础复习(重点)
一. 初识Java编程 1. Java开发环境 JDK Java开发工具 JVM Java虚拟机 JRE Java运行环境 2.实现第一个hello world public cl ...
随机推荐
- jenkins中的pipline
Jenkins Pipeline 的核心概念 Pipeline,简而言之,就是一套运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排 ...
- 【2020NOI.AC省选模拟#2】A. 旋转
题目链接 原题解: 把每个点的坐标视为复数,那么每次询问就是区间求平均数(先求和然后除以个数).一个点绕着原点旋转就是乘以$(\cos 60^\circ +i\sin 60^\circ)$. 一个点绕 ...
- OutLook从excel导入联系人
1.将已有的excel打开-->另存为-->csv格式 2.用记事本打开 .CSV 文件,选择"文件"-"另存为",修改为 ANSI编码后,然后导 ...
- 用Python中的hashlib实现md5和sha加密
文章目录 一.用 pandas 读取 Excel 数据 二.加密库 hashlib 三. pandas中的 map() 方法 四.数据加密工具 本文分享知识: pandas读取Excel数据 read ...
- iClock数据服务器默认账号密码
网络空间资产搜索: shodan 搜索 默认密码 admin/a***n End!!!
- bzoj 4195
并查集水题 离散化之后直接并查集合并,在不等时判断两者是否在同一个集合內即可 注意排序 贴代码: #include <cstdio> #include <cmath> #inc ...
- Git系列 -> 如何获取远程库某个文件
有时候远程库存放很多文件夹,我们可能只需要下载或获取某个文件中的内容,可以采用以下: 首先点击clone 按钮,然后复制clone URL , 如果想要下载其中的某个文件夹的内容,需要在后面加入指定文 ...
- oculus按键大全
// OVRInput.Update(); if (OVRInput.GetUp(OVRInput.Button.Three)) { Debug.Log("remote click" ...
- ES7-ES12总结篇
脑图模式 插入 ES7-ES12 ES7 Array.prototype.includes() includes() 方法用来判断一个数组是否包含一个指定的值,如果包含则 ...
- vue表单校验限制输入数字后小数点两位(包括避开通过中文输入法的那些坑)
<el-form-item label="海运运费系数"> <el-input v-model.trim="ruleForm.oceanFreightC ...