Class1.isassignablefrom (Class2) determines Class
whether the class or interface represented by this object is the Class
same as the class or interface represented by the specified parameter, or whether it is a superclass or a hyper-interface. returns if yes true
; false
If it Class
represents a base type, and the specified Class
parameter is exactly the Class
object, the method returns true
; false
1.class2 is not a subclass or sub-interface of Class1
2.Object is the parent class for all classes
An example is done:
PackageCom.auuzee.test1;classA {}classBextendsA {} Public classTest1 { Public Static voidMain (string[] args) {A a=NewA (); b b=NewB (); A BA=NewB (); System.out.println ("1-------------"); System.out.println (A.class. IsAssignableFrom (A.getclass ())); System.out.println (B.class. IsAssignableFrom (B.getclass ())); System.out.println (A.class. IsAssignableFrom (B.getclass ())); System.out.println (B.class. IsAssignableFrom (A.getclass ())); System.out.println (A.class. IsAssignableFrom (Ba.getclass ())); System.out.println (B.class. IsAssignableFrom (Ba.getclass ())); System.out.println ("2-------------"); System.out.println (A.getclass (). IsAssignableFrom (A.class)); System.out.println (B.getclass (). IsAssignableFrom (b.class)); System.out.println (A.getclass (). IsAssignableFrom (B.class)); System.out.println (B.getclass (). IsAssignableFrom (A.class)); System.out.println (Ba.getclass (). IsAssignableFrom (A.class)); System.out.println (Ba.getclass (). IsAssignableFrom (B.class)); System.out.println ("3-------------"); System.out.println (Object.class. IsAssignableFrom (B.getclass ())); System.out.println (Object.class. IsAssignableFrom ("ABC". GetClass ())); System.out.println ("4-------------"); System.out.println ("A". GetClass (). IsAssignableFrom (Object.class)); System.out.println ("ABC". getclass (). IsAssignableFrom (Object.class)); }}
Usage of IsAssignableFrom in Java