-
java instanceof与类型转换
instanceof与类型转换(基础篇)
instanceof可以用来判断一个对象是什么类型,判断两个类之间是否存在父子关系。
都在代码里啦
//测试类
package oop;
import oop.demo02.Person;
import oop.demo02.Student;
import oop.demo02.Teacher;
public class Application {
public static void main(String[] args) {
//在这四个类之间的关系
//Object>String
//Object>Person>Teacher
//Object>Person>Student
//用instanceof输出判断各类之间的关系
//System.out.println("x instanceof y");能不能编译通过主要看x与y是不是存在父子关系。
//结果为true还是false看x所指向的实际类型是不是y的子类型。
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Teacher);//False
System.out.println(object instanceof Object);//true
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 Teacher);//False
System.out.println(person instanceof Object);//true
//System.out.println(person instanceof String);//false编译时就会报错
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 Teacher);//False//编译报错
System.out.println(student instanceof Object);//true
// System.out.println(student instanceof String);//编译报错
}
}
//父类与子类,都继承父类
package oop.demo02;
public class Student extends Person {
}
package oop.demo02;
public class Person {
public void run(){
System.out.println("run");
//在这里是无法重写子类的方法的。
}
}
package oop.demo02;
public class Teacher extends Person {
}
类型转换
当父类转向子类需要强制转换,向下转型
子类转向父类自动转换。,向上转型
父类的引用指向子类的对象。
代码示例:
//首先我们写一个父类
package oop.demo02;
public class Person {
public void run(){
System.out.println("run");
//在这里是无法重写子类的方法的。
}
}
//再写一个子类
package oop.demo02;
public class Student extends Person {
public void go() {
System.out.println("go");
}
}//子类里面有自己的go方法和继承了父类的run方法
//写一个测试类调用
package oop;
import oop.demo02.Person;
import oop.demo02.Student;
//类型之间的转换
public class Application {
public static void main(String[] args) {
//父转子,要强转,子转父,不需要。
// Person oba = new Student();
//把student对象类型转换为Student,就可以使用Student类型的方法了
//Student student = (Student) oba;//进行强制转换。
// student.go();
Student student = new Student();
student.go();
Person person = student;//低转高
}
}
出处:https://www.cnblogs.com/kuangsun125/p/15103515.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比