Know Class Class and its application

xiaoxiao2021-03-06  114

One. Class class

1. Class Class object describes the runtime Classes and Interfaces. The relevant information of the Classes and Interfaces in the run can be obtained by the Class object. 2. Each Class has a corresponding Class object. Each Class's Class object is stored in the file where the compiled class is located. So, when the JVM is loaded with a .class file, a Class object is loaded. So, a Class has only one Class object.

two. Basic Application of Class Class

1. How to get a Class object of a class

1. This method receives a String parameter through the class.forname (ClassName) method to specify which Class object to generate., Such as class.forname ("DOG"). 1.2 is obtained by class literals. 1) The form of literal constants is: classname.class. Such as dog.class. 2) For basic classes, each basic type of overscate has a standard data called Type, which is capable of generating a Reference object to a corresponding basic type of Class object. If int.class is equivalent to Integer.Type.

1.3 is obtained via the Object.getClass () method, such as DOG DOG = New Dog (); dog.getClass (); 1.4 An example of the Class Cat {cat () {system.out.println ("init cat (" init cat) ) ")");} Static {system.out.println ("loading cat");}}} class dog {dog () {system.out.println ("init Dog ()");} static {system.out.println ("Loading Dog");}} class duck {duck () {system.out.println ("init duck ()");} static {system.out.println ("Loading Duck");}} public class test {Public static void main (string [] args) {system.out.println ("in main ()"); new cat (); system.out.println ("after create cat ()"); try {class c1 = Class.Forname ("DOG"); // (1) Class C2 = Dog.class; // (2)} Catch (CLASSNOTFOUNDEXCEPTION CNFE) {cnfe.printStackTrace ();} system.out.println ("After Class .forname (/ "DOG /") "); duck d = new duck (); system.out.println (" after create duck () "); Class C3 = d.getClass (); system.out.pri NTLN ("After Class.Forname (/" DUCK / ")");}} Run Results: in main () loading catinit cat () AFTER CREATE CAT () Loading Dogafter Class.Forname ("DOG") Loading Duckinit Duck () After Create Duck () After Class.Forname ("DUCK") code (1) (2) Generates a Class Dog's Class object, but does not produce a Class Dog object. **: Class object that generates a class does not result in a Class object 2. Type comparison with Classc objects.

2.1. Direct comparison two Class objects are compared by the equals () function or directly using the == operator, compared to whether the type is the same. 2.2. Compare, such as Class Cat {} Class Dog {} Class Duck {} PUBLIC C2, C3; DUCK D = NEW Duck (); c2 = dog.class; c3 = d.getClass (); system.out.println ("c2.isintance (d):" (c2.isinstance (d)); system.out.println "c3.isintance (d): (C3.Isinstance (D)));}} The result is: C2.IsInTance (D): falsec3.isintance (d): true2.3. Compare the INSTANCEOF keyword . However, two class objects and Class objects in different inheritance systems will compile errors. Class cat {} class dog {} class duck {} public class test {public static void main (string [] args) {class c3; // Object d = new duck (); (1) DUCK D = New Duck () ; // (2) C3 = d.getClass (); System.out.Println ("D InstanceOf Dog:" (D InstanceOf Dog); // (3)

}} Since Duck and DOG are in two different inheritance systems, code (3) will compile errors. If the code (2) comment is commented, the annotation of the code (1) is removed, and the compile will pass. This is because all CLASs are inherited from Object, so Object is in the same inheritance system in the same inheritance system.

2.4. Integrated example class base {} class derived {} public class test {public static void test (object x) {system.out.println ("Testing X of type" x.getClass ()); System.out.println ("x instanceof base"); system.out.println ("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 ("x.getclass (). Equals (base.class)" (x.getClass (). Equals (base.class)); system.out.println ("x.getclass (). Equals (DERIVED.CLASS) " (x.getclass (). Equals (derived.class)));} public static void main (ST Ring [] args) {test (new break ());}}}}}} Run Results: Testing X of type class basex instanceof base truex instanceof derived falsebase.isinstance (x) Truederived.isinstance (X Falsex.getClass () == base.class truex.getClass () == derived.class falsex.getClass (). Equals () Truex.getClass () trueX.getClass (). Equals (derived.class) Falstesting X of type class Derivedx instanceof base false.isinstance (x) falsederived.isinstance (x) trueX.getClass () == base.class falsex.getClass () ==

转载请注明原文地址:https://www.9cbs.com/read-125299.html

New Post(0)