ホームページ  >  記事  >  Java  >  JavaのinstanceOf

JavaのinstanceOf

WBOY
WBOYオリジナル
2024-08-30 15:44:53887ブラウズ

Java の

InstanceOf は、継承の概念が Java コード スニペットに実装されている場合に、オブジェクトとそのクラスの関係を決定するために使用されます。一般に、instanceOf クラスは、継承を許可するオブジェクト指向プログラミング言語に適用でき、出力値をブール結果の形式、つまり true または false のいずれかで返します。 instanceOf クラス変数が NULL 値を持つ場合、クラスは出力を「false」として返します。このクラスは比較目的で使用されるため、「型比較演算子」とも呼ばれます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

instanceOf クラスは、オブジェクトが何らかのクラスのものであるかどうかを確認するために使用されます。

obj instanceOf object

上記は、instanceOf クラスの標準構文です。ここで、obj は、以前に作成されている必要があるオブジェクトの名前です (参照)。 instanceOf はキーワードで、オブジェクトは obj オブジェクトと一致するクラスまたはインターフェイスです。

オブジェクトのコレクションがあり、それがどのオブジェクトを参照しているかわからない場合など、さまざまな場合に、instanceOf が主に役立つことが証明されます。このようなケースの例としては、多くのコントロールを備えた単純なフォームが挙げられます。

また、NULL 値を持つ変数でinstanceOfを使用した場合、必ず false を返します。

instanceOf はどのように機能しますか?

Java の instanceOf 演算子は、単純な is-a 関係で動作します。簡単に言うと、is-a 関係はオブジェクト指向の概念であり、クラス A がクラス B のサブクラスである場合に、抽象化間の関係を比較したり、たとえばその関係に取り組んだりします。これは完全に継承に基づいた関係です。つまり、「XはY型である」と言っているようなものです。

ここで、それぞれのコードとともに、instanceOf の仕組みを理解しましょう。

まず、Parent という名前のクラスを作成します。

コード:

class Parent{
}
//Then let’s add a simple main class.
public static void main(String args[]) {
}

次に、親クラスのインスタンスを作成します。

Parent child = new Parent();

最後に、instanceOf 演算子を使用して、子と親の関係を確認します。これは次のようになります: 親の子インスタンス

前述したように、instanceOf の構文は、チェックする必要があるオブジェクトから始まり、instanceOf キーワード、そして指定されたオブジェクトをテストするクラス/インターフェイスになります。

クラス宣言でキーワード「extends」または「implements」に遭遇した場合、それは is-a 関係が使用されていることを示す明らかな兆候です。

Java でのinstanceOf の例

次の例は、instanceOf の 1 行の使用法を示しています。

class instanceof_java{
public static void main(String args[]) {
instanceof_java s = new instanceof_java();
System.out.println(s instanceOf instanceof_java);
}
}

コードの解釈: 簡単なクラスのinstanceof_javaを作成することから始めました。クラスinstanceof_javaにはメインクラスがあり、メインクラス内にオブジェクトが作成されます。このオブジェクトは、instanceof_java タイプです。次に、instanceOf の動作を実装するために、オブジェクトを含む出力ステートメントを提供しました。最後の行では、instanceof キーワードおよび親クラスとともに s を渡しました。オブジェクト s は instanceof 型であるため、実行するとコードは「true」を返します。

JavaのinstanceOf

さらに進むと、既知のクラスまたはインターフェイスのオブジェクトがあっても、同じオブジェクトに値を割り当てていない場合、たとえ同じクラスであっても、必ず false が返されます。

class instanceof_sample{
public static void main(String args[]) {
instanceof_sample new = null;
System.out.println(new instanceOf instanceof_sample);
}
}

ここには、前の例と同様のコードがありますが、null 値オブジェクトが含まれています。このコードを実行すると false が返されます。ご覧のとおり、オブジェクト new はinstanceof_sampleのインスタンスですが、newにはnull値が代入されており、falseを返します。

JavaのinstanceOf

instanceOf Operator のルール

オブジェクト ref が null ではなく、参照される型のインスタンスであるかどうかに基づきます。 X が参照されるオブジェクトの単純クラス、Y が解決されたクラスまたはインターフェイス型の配列の場合。

  • When X is a simple class, then:
  • If Y is a class type, then the X must be a subclass of Y, or X must the same class as Y.
  • If Y is an interface type, then the X class must implement interface T.
  • When X is type interface, then:
  • If Y is a class type, then the Y must be an Object.
  • Y can be the same as the interface as X or super interface of X if Y is an interface type.
  • If X is a class, which is representing the array type SC[], which is an array of type SC components, then:
  • If Y is a class type, then Y must be an object.
  • If Y is an interface type, then Y must be of interface type implemented by arrays.

Finally, we will demonstrate an instanceOf program to understand that the parent object cannot be an instance of the child class.

Program

class Subject {  }
class Topic extends Subject { }
class instanceof_java
{
public static void main(String[] args)
{
Subject history = new Subject ();
if (history instanceof Topic)
System.out.println("history is instance of Topic");
else
System.out.println("history is NOT instance of Topic");
}
}

Code Interpretation: For the purpose of understanding the instanceOf operator in different scenarios, we wrote the above code. We created a simple class Subject and then another class Topic, which extends class Subject, making the class Topic as child and class Subject as Parent here. Then another class with the main method. Within the main method, we created a new instance of parent class Subject. In the IF ELSE loop, we checked the instance object’s condition with the parent class Subject. If the condition was fulfilled, it would return “history is an instance of Topic” and “history is NOT an instance of Topic” if the condition fails.

Upon executing the above code, the output will be “history is NOT an instance of Topic”, meaning the condition passed in IF fails. It happened because we tried to check the parent of the object “history” with the class Topic. We know that the class Topic extends the class Subject, meaning Topic is a subclass to Subject. And when we tried to check the type of history with class Topic, it returns false (NOT). This means the Parent Object cannot be an instance of a class.

 Output:

JavaのinstanceOf

Conclusion

We have learned about instanceOf class in Java, which simply decides if the object is of the given type. We understood how is-a relationship impacts on instanceOf operator. Also known as a comparison operator, instanceOf is based on inheritance.

以上がJavaのinstanceOfの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。