Many ways to get Class

Class class

The following methods are defined in the Object class, this method will be inherited by all subclasses

Public final Class getClass()
  • The type of the return value of the above method is a Class class. This class is the source of Java reflection. In fact, the so-called reflection is also well understood from the running results of the program, that is, the name of the class can be obtained through object reflection.

The information that an object can get after looking in the mirror: the properties, methods, constructors of a certain class, and which interfaces a certain class implements. For each class, JRE reserves an unchanging Class type object. A Class object contains information about a specific structure (class/interfcae/enum/annotation/primitive type/vodi/[])

  • Class itself is also a class
  • Class objects can only be created by the system
  • A loaded class will only have one Class instance in the JVM
  • A Class object corresponds to a .class file loaded into the JVM
  • Each type of instance will remember which Class instance it was generated by
  • Through Class, all the loaded structures in a class can be completely obtained
  • The Class class is the root of Reflection. For any class you want to dynamically load and run, you must first obtain the corresponding Class object.

Common methods of Class

Insert picture description here

Get an instance of the Class class

  • If the specific class is known, it can be obtained through the Class attribute of the class. This method is the safest and most reliable, with the highest program performance.

    Class c1=Person.class;
    
  • Knowing an instance of a certain class, call the getClass() method of the instance to obtain the Class object

    Class c2=person.getClass();
    
  • The full class name of a class is known, and the class is in the class path, which can be obtained through the static method forName() of the Class class, which may throw ClassNotFoundException

    Class c3=Class.forName("demo.Student");
    
  • The built-in basic data types can directly use the class name.

  • You can also use ClassLoader

package Reflection;




//Class类创建方式
public class Test02 {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
    
    
        Person p=new Student();
        System.out.println("这个人是:"+p.name);

        //方式一:通过对象获得
        Class c1 = p.getClass();
        System.out.println(c1.hashCode());

        //方式二:通过forName获得
        Class c2= Class.forName("Reflection.Student");
        System.out.println(c2.hashCode());

        //方式三:通过类名.class获得
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        //方式四:基本内置类型的包装类都有一个TYPE属性
        Class c4 = Integer.TYPE;
        System.out.println(c4);

        //获得父类的类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);

    }
}


class Person{
    
    
    public String name;

    public Person(String name) {
    
    
        this.name = name;
    }

    public Person() {
    
    
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person{
    
    
     public Student() {
    
    
         this.name="学生";
     }
}

class Teacher extends Person{
    
    
    public  Teacher(){
    
    
        this.name="老师";
    }
}

Guess you like

Origin blog.csdn.net/qq_45162683/article/details/112093835