1 Packageclasstest;2 3 /**4 * How to represent an instance object of a class, that is, a class type, there are three methods, or you can create an object instance of a class from a class type5 * @authorWang6 *7 */8 Public classClassDemo1 {9 Public Static voidMain (string[] args) {Ten One //how the instance object of Foo is represented, Foo1 is represented. AFoo foo1 =NewFoo (); - - //Foo This class is also an instance object, the Class class instance object, how to express, any class is an instance object of class, this instance object has three kinds of representations the //the first representation---> actually tells us that any class has an implied static member variable class -Class C1 = Foo.class; - - //the second type of representation---> already knows that the object of the class is through the GetClass method +Class C2 =Foo1.getclass (); - + //The Third Way of expression---> dynamically load classes through the Class.forName method AClass C3 =NULL; at Try { -C3 = Class.forName ("Com.imooc.reflect.Foo"); -}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } in - /* to * Official website C1, C2 indicates the class type of the Foo class (class type) Everything is object, class is an object, is an instance object of class + * This object we call the class type of the class - */ the * //whether C1 or C2 represent the class type of the Foo class, a class can only be an instance object of class $SYSTEM.OUT.PRINTLN (C1 = =C2);Panax Notoginseng -System.out.println (C2 = =C3); the + //We can create an object instance of the class entirely from the class type of the classes----> Create an Instance object of Foo from C1 or C2 or C3 A Try { the //need to have a parameterless construction method +Foo foo =(Foo) c1.newinstance (); - foo.print (); $}Catch(instantiationexception e) { $ //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(illegalaccessexception e) { the //TODO auto-generated Catch block - e.printstacktrace ();Wuyi } the - } Wu } - About classFoo { $ - voidprint () { -System.out.println ("foo"); - } A}
The use of class classes in Java