当前位置:网站首页>When using polymorphism, two ideas to judge whether it can be transformed downward
When using polymorphism, two ideas to judge whether it can be transformed downward
2022-07-23 20:16:00 【SSS4362】
When using polymorphism , Two ways to judge whether it can be transformed downward
1 Use instanceof keyword
1.1 The core idea
Determine whether the object is an instance of a class ( Such as animal01 Whether it is Dog Generated by instantiation )
1.2 Sample code
Animal class
package Work;
public class Animal {
private String name;
// full name
private int age;
// Age
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Dog class
package Work;
public class Dog extends Animal{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void guardDoor(){
System.out.println(" The dog is watching the door ");
}
}
Test class
package Work;
public class Test {
public static void main(String[] args) {
Animal animal01=new Dog(" Wangcai ",10);
if (animal01 instanceof Dog){
((Dog) animal01).guardDoor();
}
}
}
1.3 Screenshot of sample code operation

2 Use getclass() Method
2.1 The core idea
Judge whether the class that generates the object is a class ( Such as animal01 Whether it is Dog Generated by instantiation )
2.2 Sample code
Animal class
package Work;
public class Animal {
private String name;
// full name
private int age;
// Age
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Dog class
package Work;
public class Dog extends Animal{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void guardDoor(){
System.out.println(" The dog is watching the door ");
}
}
Test class
package Work;
public class Test {
public static void main(String[] args) {
Animal animal01=new Dog(" Wangcai ",10);
if (animal01.getClass()==new Dog().getClass()){
((Dog) animal01).guardDoor();
}
}
}
2.3 Screenshot of sample code operation

边栏推荐
- I deliberately leave a loophole in the code. Is it illegal?
- 17.生命周期
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记四
- [unity project practice] level unlocking
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- Osgearth2.8 compiling silvering cloud effect
- Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
- [unity project practice] entrustment
- Leetcode 238. product of arrays other than itself
- 20. Ref and props
猜你喜欢
随机推荐
TASK03|回归
Redis坏了怎么办?
JDK安装包和Mysql安装包整理
2022/7/21训练总结
2022上半年中国十大收缩行业
Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
Leetcode 219. 存在重复元素 II(可以,已解决)
Baidu map data visualization
I deliberately leave a loophole in the code. Is it illegal?
New product listing | A-share floor derivatives market point
数仓4.0笔记——数仓环境搭建—— DataGrip准备和数据准备
Configure MySQL master-slave replication with mysqldump or mydumper
QT With OpenGL(帧缓存篇)
使用Jmeter和VisualVW进行压测准备
Osgearth2.8 compiling silvering cloud effect
absl教程(四):Strings Library
梅科尔工作室-小熊派开发笔记2
Relevant interfaces of [asp.net core] option mode
能量原理與變分法筆記19:最小餘能原理+可能功原理
Odrive application 6 encoder




![[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)



