JAVA programming this morning to see it thinking of the son, a long time ago some debugging, but always lazy, did not see very clearly that today it Nien on the blog, so forget the old.
JAVA examples about the determination of the type of operation, (RTTI)
Package thinkInJava;
Class Base ();
Class Derived extends Base ();
(Public class FamilyVsExactType
Static void test (Object x)
(
System.out.println ( "Begin:");
System.out.println ( "Testing x of type" + x.getClass ());
System.out.println ( "x instanceof Base" + (x instanceof Base));
System.out.println ( "x instanceof Derived" + (x instanceof Derived));
System.out.println ( "Base.isInstance (x)" + Base.class.isInstance (x));
System.out.println ( "Derived.isInstance (x)" + Derived.class.isInstance (x));
System.out.println ( "x.getClass () == Base.class" + (x.getClass () == Base.class));
System.out.println ( "x.getClass () == Derived.class" + (x.getClass () == Derived.class));
System.out.println ( "Base.class =" + Base.class);
System.out.println ( "Derived.class =" + Derived.class);
System.out.println ( "x.getClass (). Equals (Base.class))" + (x.getClass (). Equals (Base.class)));
System.out.println ( "x.getClass (). Equals (Derived.class))" + (x.getClass (). Equals (Derived.class)));
System.out.println ( "End!");
)
/ **
* @ Param args
* /
Public static void main (String [] args) (
/ / TODO Auto-generated method stub
Test (new Base ());
Test (new Derived ());
)
)

Base Derived x.getClass () class thinkInJava.Base class thinkInJava.Derived x instanceof Base true true x instanceof Derived false true Base.class.isInstance (x) true true Derived.class.isInstance (x) false true x.getClass () == Base.class true false x.getClass () == Derived.class false true x.getClass (). equals (Base.class) true false x.getClass (). equals (Derived.class) false true Base.class class thinkInJava.Base Derived.class class thinkInJava.Derived
Package thinkInJava; import java.util.Random; class Pet (); class Dog extends Pet (); class Pug extends Dog (); class Cat extends Pet (); class Rodent extends Pet (); class Gerbil extends Rodent (); class Hamster extends Rodent (); interface testInterface (); class Counter (inti inti; public String toString () (return Integer. toString (i );}}; class AssociativeArray (private Object [] [] pairs; privateintindex privateintindex privateintindex; public AssociativeArray (int length) (pairs = new Object [length] [2];) publicvoid publicvoid put (Object key, Object value) (if (index> = pairs.length) thrownew thrownew ArrayIndexOutOfBoundsException (); pairs [index + +] = new Object [] (key, value);) public Object get (Object key) (for (int i = 0; i <index; i + +) (if (key.equals (pairs [i] [0])) returnpairs returnpairs [i] [1];) thrownew thrownew RuntimeException ( "Failed to find key");) public String toString () (String result = ""; for (int i = 0; i <index; i + +) (result + = pairs [i] [0] + ":" + pairs [i] [1]; if (i <index -1) result + = "\ n";) return result;)) (publicclass publicclass Rtti privatestatic privatestatic Random rand = new Random (); typenames static String [] = ( "Pet", "Dog" and "Pug", "Cat", "Rodent", "Gerbil", "Hamster",); / ** * @ paramargs paramargs * / publicstaticvoid publicstaticvoid publicstaticvoid main (String [] args) (/ / TODO Auto-generated method stub Object [] = new Object pets [15]; try (Class [] = (Class petTypes. forName ( "thinkInJava.Dog") , Class. forName ( "thinkInJava.Pug"), Class. forName ( "thinkInJava.Cat"), Class. forName ( "thinkInJava.Rodent"), Class. forName ( "thinkInJava.Gerbil"), Class. forName ( " thinkInJava.Hamster "),); for (int i = 0; i <pets.length; i + +) (pets petTypes [i] = [rand. nextInt (petTypes.length)]. newInstance ();)) catch (Exception e) () AssociativeArray map = new AssociativeArray (typenames. length); for (int i = 0; i <typenames. length; i + +) (map.put (typenames [i], the new Counter ());) for (int i = 0; i <pets.length; i + +) (Object o = pets [i]; if (o instanceof Pet) ((Counter) map.get ( "Pet").) i + +; if (o instanceof Dog) ( (Counter) map.get ( "Dog").) i + +; if (o instanceof Pug) ((Counter) map.get ( "Pug").) i + +; if (o instanceof Cat) ((Counter) map.get ( "Cat").) i + +; if (o instanceof Rodent) ((Counter) map.get ( "Rodent").) i + +; if (o instanceof Gerbil) ((Counter) map.get ( "Gerbil")) . i + +; if (o instanceof Hamster) ((Counter) map.get ( "Hamster").) i + +;) for (int i = 0; i <pets.length; i + +) (System. out. println (pets [ i]. getClass ());) System. out. println (map); System. out. println (testInterface. class); new Pet Pet _pet = (); System. out. println (_pet); System. out. println (_pet.getClass ()); Dog _dog = new Dog (); System. out. println (_dog); System. out. println (_dog.getClass ()); try (System. out. println (Class. forName ( "thinkInJava.Dog"). getName ());) catch (ClassNotFoundException e) () Object [] = (_pet object, _dog); for (int i = 0; i <object.length; i + +) (System. out. println (object.getClass ());))
)
String java.lang.Class.getName () getNamepublic getNamepublic String getName () Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by the Java Language Specification, Second Edition. If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void. If this class object represents a class of arrays, and then the internal form of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. The encoding of element type names is as follows:
Element Type Encoding boolean Z byte B char C class or interface Lclassname Lclassname; double D float F int I long J short S
The class or interface name classname is the binary name of the class specified above. Examples: String.class.getName () returns "java.lang.String" byte.class.getName () returns "byte" (new Object [3] ). getClass (). getName () returns "[Ljava.lang.Object;" (new int [3] [4] [5] [6] [7] [8] [9]). getClass (). getName () returns "[[[[[[[ I "
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Tags: java runtime, Runtime
Releated Java Articles
November 7, 2006 | Filed Under
API for Java |
Comments