当前位置:网站首页>Instanceof and type conversion
Instanceof and type conversion
2022-06-11 06:01:00 【visionkiwi】
Instanceof And type conversion
Instanceof
The main function
package oop;
import oop.Demo05.Person;
import oop.Demo05.Student;
import oop.Demo05.Teacher;
// There is only one specification for a project main Method
public class Application {
public static void main(String[] args) {
/* Be careful : The students and teachers here because in the code , Students and teachers have no code relationship , Just inherited the human class */
//Object > String
//Object > Person > Student
//Object > Person > Teacher
// This is another route from Student It doesn't matter, so it is false
// This is to determine whether an object is an instance of an object or any of its base classes , Here object It's actually Student Class
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); // Compile and report errors , because Person and String Is at the same level ,
// There is no connection between them , Two lines
/* //System.out.println(x instanceof y); If x and y There is an inheritance relationship , Then at least the compiler will not report an error , But if at this time x yes y Parent class of , because instanceof The comparison is to go back up one by one for comparison */
}
}
Parent class
package oop.Demo05;
public class Person {
public void run(){
System.out.println("run");
}
}
Subclass Student
package oop.Demo05;
public class Student extends Person{
}
Subclass Teacher
package oop.Demo05;
public class Teacher extends Person{
}
Type conversion
The main function
package oop;
import oop.Demo05.Person;
import oop.Demo05.Student;
import oop.Demo05.Teacher;
// There is only one specification for a project main Method
public class Application {
public static void main(String[] args) {
// Conversion between types : Basic type conversion High and low 64 32 16 8
// Conversion between types : Father Son
//Person Class height Studnet Class low
// So the conversion from low to high does not need to be forced
Person qwe = new Student();
/*qwe.go();// because Person There is no go Method , // Student Class go Method , So no */
// So now we will qwe This object is converted to Student type ,
// Can Student Method of type
// So this is a high to low , A cast is required
Student student = (Student) qwe;
student.go();
// or
((Student)qwe).go();
// Low to high ( Converting a subclass to a parent class may lose some of the original methods )
Student student1 = new Student();
student1.go();
Person person = student1;
//person.go();// Lost method , So wrong reporting
}
}
/* 1. The reference of the parent class points to the object of the child class 2. Convert a subclass to a parent , Upward transformation 3. Convert a parent class to a child class , Move down : Coercive transformation ( Will lose precision , Is not very good ) 4. Convenient method call , Reduce duplicate code abstract : encapsulation 、 Inherit 、 polymorphic */
Parent class
package oop.Demo05;
public class Person {
public void run(){
System.out.println("run");
}
}
Subclass Student
package oop.Demo05;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
Subclass Teacher
package oop.Demo05;
public class Teacher extends Person{
}
@ Meet frenzy
边栏推荐
- Clojure installation of metabase source code secondary development
- Distributed framework ray - detailed introduction to starting ray and connecting clusters
- Adapter the problem of executing only one animation in multiple frames
- 亚马逊、速卖通、Lazada、虾皮平台在用911+VM的环境可以进行产号、养号、补单等操作吗?
- NFC Development -- difference between ID card and IC card (M1 card and CPU card) (III)
- NDK learning notes (XI) POSIX sockect local communication
- Sword finger offer 04: find in 2D array
- [IOS development interview] operating system learning notes
- Use of vlayout
- Do we really need conference headphones?
猜你喜欢

JIRA software annual summary: release of 12 important functions

"All in one" is a platform to solve all needs, and the era of operation and maintenance monitoring 3.0 has come

Error:Execution failed for task ':app:buildNative'. & gt; A problem occurred'x/x/x/'NDK build' error resolution

Installing and using sublist3r in Kali

做亚马逊测评要了解的知识点有哪些?

Can Amazon, express, lazada and shrimp skin platforms use the 911+vm environment to carry out production number, maintenance number, supplement order and other operations?
![[metadata]linkedin datahub](/img/6a/247ee77dfc219d26651e5d93b04c98.png)
[metadata]linkedin datahub

NDK learning notes (VIII) thread related

Global case | how an airline with a history of 100 years can expand and transform to promote innovation in the aviation industry

This is probably the most comprehensive project about Twitter information crawler search on the Chinese Internet
随机推荐
ELK日志系统实战(五):安装vector并将数据输出到es、clickhouse案例
安装Oracle数据库
What is a planning BOM?
NDK learning notes (x) POSIX socket for connectionless communication
[daily exercises] 1 Sum of two numbers
Moteur de modèle de moteur thymeleaf
How to use the markdown editor
Compliance management 101: processes, planning and challenges
使用Batch管理VHD
When the recyclerview plays more videos, the problem of refreshing the current video is solved.
handler
Sqli-libs post injection question 11-17 actual combat
数组部分方法
Install Oracle Database
Gilde failed to go to the listener to call back the reason record when loading the Gaussian blur image
Further efficient identification of memory leakage based on memory optimization tool leakcanary and bytecode instrumentation technology
Multi thread tutorial (30) meta sharing mode
Free get | full function version of version control software
Summarize the five most common BlockingQueue features
OJDBC在Linux系统下Connection速度慢解决方案