📄 comparetest.java
字号:
package Operator;
import java.awt.*;
/*
The instanceof comparison operator is used for object variables only.
The purpose of this operator is to determine whether an object belongs
to a given class (or any of the superclasses). This comparison may not
be made on primitive types and will result in a compile-time error
if it is attempted.
*/
class CompareTest {
public static void main(String [] args) {
Button b = new Button("Exit");
boolean compare1 = b instanceof Button;
boolean compare2 = b instanceof Component;
System.out.println("Is b a Button? " + compare1);
System.out.println("Is b a Component? " + compare2);
}
}
/*
There is a very important rule when using instanceof.
It is not legal to attempt to compare an instance to a class
if the instance is not in the same object hierarchy as the class.
If we attempt to compare an instance with a class that is not
part of the hierarchy, the compiler will produce an error.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -