java --- isAssignableFromメソッドの説明



Java Isassignablefrom Method Explanation



isAssignableFromメソッドはネイティブメソッドです。その機能は、2つのクラス間の関係を判別することです。クラスを別のインスタンスオブジェクトに強制変換できるかどうかも言えます。

/** * Determines if the class or interface represented by this * {@code Class} object is either the same as, or is a superclass or * superinterface of, the class or interface represented by the specified * {@code Class} parameter. It returns {@code true} if so * otherwise it returns {@code false}. If this {@code Class} * object represents a primitive type, this method returns * {@code true} if the specified {@code Class} parameter is * exactly this {@code Class} object otherwise it returns * {@code false}. * *

Specifically, this method tests whether the type represented by the * specified {@code Class} parameter can be converted to the type * represented by this {@code Class} object via an identity conversion * or via a widening reference conversion. See The Java Language * Specification , sections 5.1.1 and 5.1.4 , for details. * * @param cls the {@code Class} object to be checked * @return the {@code boolean} value indicating whether objects of the * type {@code cls} can be assigned to objects of this class * @exception NullPointerException if the specified Class parameter is * null. * @since JDK1.1 */ public native boolean isAssignableFrom(Class<?> cls)

翻訳されたコメントのメソッドの説明は次のとおりです。



クラスタイプには2つのクラスアイコンがあります。1つはisAssignableFromメソッドを呼び出すクラスオブジェクト(以下、オブジェクトaと呼びます)と、メソッドのパラメータとしてのクラスオブジェクト(オブジェクトbと呼びます)です。これらの2つのオブジェクトが次の条件を満たす場合は、trueを返し、そうでない場合はfalseを返します。

  • オブジェクトに対応するクラス情報は、bオブジェクトに対応するクラス情報の親クラスまたは親インターフェースです。簡単な理解は、aがbの親クラスまたはインターフェースであるということです。
  • aオブジェクトに対応するクラス情報は、bオブジェクトに対応するクラス情報と同じです。簡単な理解は、aとbが同じクラスまたは同じインターフェースであるということです。