This article mainly introduces the Java language CLASS classification and generalization (detailed explanation). Everyone knows that the Java program performs type identification of all objects during operation, that is, RTTI. This information records the class of each object. The virtual machine usually uses the correct method of running the type information to be executed. The class used to preserve these types of information is the Class class. The Class class encapsulates a state and the state of the interface is running. When the loading class, the Class type object is automatically created. The specific content is described as follows:
To put it plainly:
The Class class is also a class, but the name is similar to that of the Class keyword. Java is a sensitive language.
The content of the class class is the type information of the class you created. For example, if you create a Shapes class, then Java will generate a class of the class class of Shapes
Objects of the class class cannot be created in a new shapes () manner like an ordinary class. Its object can only be created by JVM, because this class does not have the Public constructor function function function.
The role of the Class class is to provide or obtain a type information that provides or obtain a certain object during runtime, which is similar to the Typeid () function in C ++. This information can also be used for reflection.
1.class class principle
We all know that all the Java classes inherit the Object class. There is a method in the Object class: getClass (). This method is used to obtain the reference to the class that has been instantly instantiated. Quote pointed to the object of the Class class. We cannot generate a Class object (the constructor is Private), and the object of this class class is to automatically create the Class object by the Java virtual machine when it is transferred, or it is generated by the defineClass method in the loader. The objects we generate will have a field to record the location of the object of the object in the class class of the object. As shown in the figure below:
2. Get a Class class object
The first method is the forname function of the class class
Copy code code as follows:
public class shapes {}
Class obj = class.Forname ("shapes");
The second method is to use the object of the object of the object
Copy code code as follows:
public class shapes {}
shapes s1 = new shapes ();
Class obj = s1.getClass ();
Class obj1 = s1.getsuperclass (); // This function is the type of the parent class of the Shapes class
The third method is to use the type of type constant
Copy code code as follows:
Class obj = string.class;
Class obj1 = int.class;
Note that when using this method to generate the Class class object, JVM will not automatically load the class (such as String). Other methods will make JVM initialize this class.
3. Use the object of the Class class to generate an instance of the target class
Inaccurate Object instance
After obtaining an object of the class class, you can use the newInstance () function to generate an instance of the target class. However, this function does not directly generate instances of the target class, and can only generate instances of the Object class
Copy code code as follows:
Class obj = class.Forname ("shapes");
Object shapesinstance = obj.newinstance ();
Use generalization Class to reference the target instance of the type belt type
Copy code code as follows:
Class <shapes> obj = shapes.class;
shapes newShape = Obj.newinstance ();
Because of the type limit, the object reference to the generalization Class syntax cannot be directed to other categories.
Copy code code as follows:
Class obj1 = int.class;
Class <integer> obj2 = int.class;
obj1 = double.class;
//Obj2=double.class;
This line of code is illegal. OBJ2 cannot be changed to other classes. However, there is a flexible usage that allows you to point to any subclasses in the base class with the object of Class.
Copy code code as follows:
Class <? Extends Number> Obj = int.class;
obj = number.class;
obj = double.class;
Therefore, the CLASS object generated below can point to any class.
Copy code code as follows:
Class <?> Obj = int.class;
obj = double.class;
obj = shapes.class;
The last strange usage is that when you use this generic syntax to build a base class object of an object of a class class you have at hand, you must use the following special grammar
Copy code code as follows:
public class shapes {}
Class Round Extends Shapes {}
Class <rand> rClass = round.class;
Class <? Super Round> sclass = rclass.GetsuperClass ();
// class <shapes> sclass = rclass.getsuperclass ();
You need to note here that you know that the base class of Round is Shapes, but you cannot directly declare the class <shapes>. You must use special grammar Class <? Super Round>
The introduction of the above content is the entire content of the Java language class classification method and generalization (detailed explanation). I hope everyone likes the above content introduction.