首页 > 其他分享 >instanceof 和类型转换

instanceof 和类型转换

时间:2024-01-13 22:33:26浏览次数:25  
标签:instanceof 类型转换 System Person Student println out

注意点

  1. 父类引用指向子类的对象

  2. 把子类转换为父类,向上转型;

  3. 把父类转换为子类,向下转型;强制转换

  4. 方便方法的调用,减少重复的代码!简洁

封装、继承、多态! 抽象类,接口

快捷键

补充语句

 

 

举例

转换类型之后使用方法

 

输出结果

 

这样改写,输出结果一样

 

代码

//Java-零基础学习/src/oop/demo06/Student
package oop.demo06;

public class Student extends Person {

   public void go() {
       System.out.println("go");
  }
}

/*
public static void main(String[] args) {

       //一个对象的实际类型是确定的
       //new Student();
       //new Person();

       //可以指向的引用类型就不确定了:父类的引用指向子类

       //Student 能调用的方法都是自己的或者继承父类的!
       Student s1 = new Student();
       //Person 父类型,可以指向子类,但是不能调用子类独有的方法
       Person s2 = new Student();
       Object s3 = new Student();

       //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
       s2.eat();//子类重写了父类的方法,执行子类的方法
       s1.eat();

   }
*/

/*
public static void main(String[] args) {

       //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);//编译报错!


   }
*/
//Java-零基础学习/src/oop/demo06/Person
package oop.demo06;

public class Person {

   public void run() {
       System.out.println("run");
  }
}

/*
public static void main(String[] args) {

       //类型之间的转化:父   子

       //高                 低
       Person obj = new Student();

       //student将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
       Student student = (Student) obj;
       student.go();
   }
*/

/*
public static void main(String[] args) {

       //类型之间的转化:父   子

       //子类转换为父类,可能丢失自己的本来的一些方法!
       Student student = new Student();
       student.go();
       Person person = student;
   }
*/
//Java-零基础学习/src/oop/demo06/Teacher
package oop.demo06;

public class Teacher extends Person {

}
//Java-零基础学习/src/oop/Application
package oop;

import oop.demo06.Person;
import oop.demo06.Student;
import oop.demo06.Teacher;

//一个项目应该只存在一个main方法
public class Application {

   public static void main(String[] args) {

       //类型之间的转化:父   子

       //子类转换为父类,可能丢失自己的本来的一些方法!
       Student student = new Student();
       student.go();
       Person person = student;
  }
}
 

标签:instanceof,类型转换,System,Person,Student,println,out
From: https://www.cnblogs.com/poiuyjoey/p/17963129

相关文章

  • instanceof原理
    instanceof作用:用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上详细来说就是,instanceof运算符是检查一个实例对象的原型链上是否出现了构造函数的原型对象,如果实例对象的原型链中出现了构造函数的原型对象,那么这个实例对象就是构造函数的一个实例,返回true,否......
  • 数据类型转换&表达式&运算符总结
    总结数据类型转换概念:将数据从一种格式或结构转换为另一种格式或结构的过程。作用:节约内存空间将一些类型转换为项目所需要的类型类型转换分类自动隐式转换定义:将小的数据类型转换大的数据类型注意事项:在Java中,boolean类型与所有其他7种类型都不能......
  • 【类型转换】使用c#实现简易的类型转换(Emit,Expression,反射)
    引言哈喽。大家好,好久不见,最近遇到了一个场景,就是在FrameWork的asp.netmvc中,有个系统里面使用的是EntityFramework的框架,在这个框架里,提供了一个SqlQuery的方法,这个方法很好用啊,以至于在EFCORE8里面又添加了回来,不过不知道性能怎么样,我遇到的场景是通过SqlQuery查询的时......
  • Numpy中数据类型转换的tips
    在逛StackOverflow时看见一个关于numpy的浮点数据转换的问题比较有趣,现当作tips记录下来。问题原地址我们知道,在numpy中,浮点数据同python本身一样,是用双精度(float64)来存储数据的,而Pytorch或者其他的一些框架中,为了节省运算量,其浮点是用单精度(float32)来存储数据的,因此需要用到数......
  • 数据类型转换的坑
    请求其他服务的接口,返回的数据类型为Map<String,Object>其中的某个key对应的Object中的类型为Double,于是在解析时直接用(Double)value进行强制类型转换然而啊然而后来被调用服务有所改动,Double类型变为Integer,于是出现强制类型转换错误为了避免此种情况出现,最好的办法是将......
  • [C++] 强制类型转换(dynamic_cast和dynamic_Pointer_cast)
    作者:丶布布1、指引或者引用的向上转换,向下转换例如基类Father,Son继承Father,派生类Son.。Father—>Son则为向下转换,Son—>Father则为向上转换。向上转换为隐士转换,向下转换需要dynamic_cast或者c的转换方式。向上转换:structFather{//基类Father};structSon:Father{//......
  • [C++ 从入门到精通] 6.static_cast、dynamic_cast等显示类型转换
    作者:丶布布文章预览:一.隐式类型转换二.显式类型转换(强制类型转换)static_cast显示转换dynamic_cast显示转换const_cast显示转换reinterpret_cast显示转换三.总结一.隐式类型转换含义:隐式类型转换:系统自动进行,不需要开发人员介入。intm=3+45.6;//48因为返回的int型......
  • JavaScript中的instanceof运算符
    JavaScript中的instanceof运算符:https://blog.csdn.net/weixin_43263355/article/details/123551619?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170320923016800197085509%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170320......
  • Day04类型转换
    类型转换注意点:1.不能对布尔值进行转换2.不能把对象类型转换为不相干的类型3.在把高容量转换到低容量的时候,强制转换4.转换的时候可能存在内存溢出,或者精度问题!高转低,强制转换;低转高,自动转换低------------------------------------------->高byte,short,char->int-......
  • 浅谈C++类型转换函数
    reinterpret_castreinterpret_cast<newtype>(expression)将一个类型的指针转换为另一个类型的指针,它允许从一个指针转换为整数类型。voidtest01(){ chara=0; int*p=reinterpret_cast<int*>(&a); //不安全}const_cast常量const指针与普通指针之间的相互转化。如果不用......