Java language class class usage and generalization (detailed) _java

Source: Internet
Author: User

This article mainly introduces the Java language class class usage and generalization (detailed), we all know that Java program in the running process, all the object type identification, that is, Rtti. This information records the class to which each object belongs. A virtual machine typically uses Run-time type information to select the correct method to execute, and the class used to hold the type information is class. Class class encapsulates the state of the runtime of an object and interface, and when the class is loaded, the object of class type is automatically created, as described below:

To be blunt:

Class classes are also one of the classes, except that the name and class keywords are highly similar. Java is case sensitive language.
The object content of class classes is the type information of the class you created, for example, you create a shapes class, then Java generates an object that is the class of the shapes.
Objects of class classes cannot be created in the form of new shapes (), like normal classes, whose objects can only be created by the JVM because the class does not have a public constructor
The class class is useful when the runtime provides or obtains the type information of an object, similar to the typeid () function in C + +. This information can also be used for reflection.

Principle of 1.Class Class

We all know that all Java classes inherit the class of object, and there is a method in the class object: GetClass (). This method is used to obtain a reference to the class of objects that have been instantiated by the class, which points to objects of class classes. We ourselves cannot generate a class object (the constructor is private), and the class object is generated by the Java virtual machine automatically creating a class object when various types are tuned in, or by the DefineClass method in the class loader. The objects we generate will have a field that records where the object belongs to the class in the class. As shown in the following illustration:

2. Get a Class object

The first approach, the forname function of class classes

Copy Code code as follows:

public class shapes{}
Class obj= class.forname ("shapes");

The second approach uses the GetClass () function 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 to get the type of the parent class of the Shapes class

the third approach, using class literal constants

Copy Code code as follows:

Class Obj=string.class;
Class Obj1=int.class;

Note that using this method to generate a class object does not cause the JVM to automatically load the class (such as the String Class). Other methods will allow the JVM to initialize the class.

3. Use objects of class classes to generate instances of the target class

Generate an inexact instance of object

After you get an object of a class class, you can use the newinstance () function to generate an instance of the target class. However, the function does not directly generate instances of the target class, only instances of the object class can be generated

Copy Code code as follows:

Class obj=class.forname ("shapes");
Object shapesinstance=obj.newinstance ();

Generating a band-type target instance using generalization class references

Copy Code code as follows:

Class<shapes> Obj=shapes.class;
Shapes Newshape=obj.newinstance ();

Because of the type restrictions, object references that use the Generalization class syntax cannot point to other classes.

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 and OBJ2 cannot be directed to another class.
However, there is a flexible usage that allows you to use class objects to point to any subclass of the base class.

Copy Code code as follows:

class<? Extends number> Obj=int.class;
Obj=number.class;
Obj=double.class;

Therefore, the class object generated by the following syntax 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 construct the base class object of a class object that you have at hand, you must use the following special syntax

Copy Code code as follows:

public class shapes{}
Class Round extends shapes{}
Class<round> Rclass=round.class;
class<? Super round> sclass= Rclass.getsuperclass ();
Class<shapes> Sclass=rclass.getsuperclass ();


Here you need to note: Know round is the base class is the shapes, but can not directly declare class < shapes, you must use special syntax class < super round >

The above description is for Java language class usage and generalization (detailed) all content, I hope you like the above introduction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.