comparetest.java

来自「主要是对于JAVA的编程的基本语言 希望能够帮得上你。」· Java 代码 · 共 31 行

JAVA
31
字号
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 + =
减小字号Ctrl + -
显示快捷键?