if (o instanceof Cat)
System.out.println("Cat");
if (o instanceof Dog)
System.out.println("Dog");
if (o instanceof Bird)
System.out.println("Bird");
if (o instanceof Lamp)
System.out.println("Lamp");
}
它这里 的意思是什么 我一脸懵逼 这题 就没想到可以这样写
求大佬 救救迷茫的我
已解决
评论 (6)
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
Dima109🔭🚁🇨🇳
22 四月, 02:29
它这玩意儿不识别中文 就很无语;但是实例里面有提示的是:Lamp为灯;这简直就是误导人
0
Gellert Varga
19 二月 2022, 09:56
Please publish the full programme.
Please be more specific about what you want to ask, because unfortunately I don't understand.
0
代码没写完啊~
21 二月 2022, 01:05
public class Solution {
public static void main(String[] args) {
printObjectType(new Cat());
printObjectType(new Bird());
printObjectType(new Lamp());
printObjectType(new Cat());
printObjectType(new Dog());
}
public static void printObjectType(Object o) {
//在此编写你的代码
if (o instanceof Cat) System.out.println("Cat");
if (o instanceof Dog) System.out.println("Dog");
if (o instanceof Bird) System.out.println("Bird");
if (o instanceof Lamp) System.out.println("Lamp");
}
public static class Cat {
}
public static class Dog {
}
public static class Bird {
}
public static class Lamp {
}
}
0
代码没写完啊~
21 二月 2022, 01:06
• 程序必须在屏幕上显示文本。
• 不要更改 Cat 类。
• 不要更改 Dog 类。
• 不要更改 Bird 类。
• 不要更改 Lamp 类。
• printObjectType() 方法必须显示以下消息之一,具体取决于传递给它的对象。例如,对于 Solution.Lamp 对象,它应该显示“灯”。
0
代码没写完啊~
21 二月 2022, 01:07
大佬 它这里的 if条件 是通过了 什么来给 这几个类 传递了参数
0
Gellert Varga
21 二月 2022, 22:07解决方法
The printObjectType method can accept any Object type object.
The Cat, Bird, Dog classes are all descendants of the Object class, so we can pass any object to the printObjectType method.
Let's take an example: a new Cat object is passed to the method.
Within the method, this Cat object is stored in a variable of type Object, in the variable "o".
The "instanceof" operator is able to examine this object "o" and is able to decide if it is an instance of the Cat class or not.
+2