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; }
}
多态小结:
1、前提:父类引用指向子类对象
2、把子类转换为父类,向上转型:会丢失一些独有的方法
3、把父类转换为子类,向下转型:强制转换
好处:
方便方法的调用,减少重复的代码,可以有效的提升利用率!是代码变的简洁
抽象:编程思想:三大特性,封装、继承、多态! 后面有抽象类!接口!

12.instanceof和类型转换的更多相关文章

  1. 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验

      12.数据类型转换.格式化.校验 12.1.数据绑定流程     Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...

  2. instanceof 和类型转换

    instanceof 和类型转换 instanceof 判断a 和 B 类型是否相似 公式 System.out.println(a instanceof B); //true / false 编译是 ...

  3. Java面向对象系列(11)- instanceof和类型转换

    instanceof 先看引用类型的类和instanceof比较的类有没有父子关系,有则可以编译,IDEA不报错 new一个对象,对象new所在的类和instanceof比较的类有没有父子关系,有则为 ...

  4. instanceof和类型转换

    什么是instanceof 判断一个对象是什么类型 注意点 X 和 Y 必须要有父子关系 否则编译都会失败 X对象只要是Y的子类(无论 是 儿子 还是 孙子 还是 曾孙....)X instanceo ...

  5. 多态(instanceof)

    多态调用的三种格式 * A:多态的定义格式: * 就是父类的引用变量指向子类对象 父类类型 变量名 = new 子类类型(); 变量名.方法名(); * B: 普通类多态定义的格式 父类 变量名 = ...

  6. SQL类型转换以及自动在前面补0满足10位工号标示法

    1,自动在前面补0满足10位工号标示法 SELECT rtrim(ltrim(right(cast('00000000'+rtrim(CAST(数值 as int)) as varchar(20)), ...

  7. Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数

    由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实 ...

  8. C++中的新型类型转换

    1,C 语言中已经有类型之间的强制转换,C++ 做了改善: 2,C 方式的强制类型转换: 1,(Type) (Expression): 2,Type (Expression): 1,这种方式和上述方式 ...

  9. JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]

    类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...

随机推荐

  1. H3C 各类路由默认优先级

  2. H3C端口状态迁移

  3. asp dotnet core 支持客户端上传文件

    本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...

  4. 前端css图片固定宽高问题

    img需要宽高都固定时,图片往往会因此变形,此时可采用的方法有: 上述代码会使得图片居中,边缘部分不显示.这是在图片大小跟container大小差不多的情况下.如果图片很大的话,只显示中心部分是不行的 ...

  5. Linux 内核bin+attribute 结构二进制属性

    sysfs 惯例调用所有属性来包含一个单个的人可读文本格式的值. 就是说, 只是偶然地很 少需要来创建能够处理大量二进制数据的属性. 这个需要真正地只出现在必须传递数据, 不可动地, 在用户空间和设备 ...

  6. clickhouse创建视图SQL 错误 [47]: ClickHouse exception, code: 47

    使用clickhouse创建视图时报错 SQL 错误 [47]: ClickHouse exception, code: 47, host: localhost, port: 8123; Code: ...

  7. mac如何查看已连接wifi的密码

    可以通道mac自带的“钥匙串访问”功能查看.选择需要查询的wifi名称,右击选择“将密码拷贝到剪贴板”,输入管理员密码后,密码就拷贝好了. 找个地方粘贴即可看到密码

  8. Jmeter阶梯加压监听

    巧用beanshell,做阶梯加压监听 1. 首先先添加阶梯加压线程组  bzm - Concurrency Thread Group 设置阶梯加压值,目标最大并发用户为80,加速步率时长为100秒, ...

  9. 经典问题(c++/python)素数、杨辉三角(金字塔型)、统计单词数、简单计算器、密码安全程度、凯撒密码加密、汉诺塔 (python课设实验实例)-- biaobiao88

    [编写程序,输人一个大于2的自然数,然后输出小于该数字的所有素数组成的列表.]所谓素数,是指除了1和自身之外没有其他因数的自然数,最小的素数是2,后面依次是3.5.7.11.13... c++代码: ...

  10. k8s集群———单master节点2node节点

    #部署node节点 ,将kubelet-bootstrap用户绑定到系统集群角色中(颁发证书的最小权限) kubectl create clusterrolebinding kubelet-boots ...