Home  >  Article  >  Java  >  Java uses the getClass() function of the Object class to obtain the runtime class of the object

Java uses the getClass() function of the Object class to obtain the runtime class of the object

PHPz
PHPzOriginal
2023-07-24 11:37:52972browse

Java uses the getClass() function of the Object class to obtain the runtime class of the object

In Java, each object has a class, which defines the properties and methods of the object. We can use the getClass() function to get the runtime class of an object. The getClass() function is a member function of the Object class, so all Java objects can call this function. This article will introduce how to use the getClass() function and give some code examples.

It is very simple to use the getClass() function to obtain the runtime class. We only need to call the object's getClass() function, which will return a Class object that represents the runtime class of the object. The following is an example:

public class Person {
    private String name;
    private int age;
    
    // 构造函数
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // Getter和Setter方法
    // ...
    
    public static void main(String[] args) {
        Person person = new Person("John", 25);
        Class<? extends Person> clazz = person.getClass();
        System.out.println(clazz.getName());
    }
}

In the above example, we create an object person of the Person class, call the getClass() function of the person object, and assign the return value to the variable clazz of the Class type. Then, we get the name of the runtime class of the person object by calling the getName() function of clazz.

It should be noted that the getClass() function returns a Class object, which contains information about the class to which the object belongs. We can call many useful functions through this object to obtain the class name, modifiers, parent class, interface, constructor, fields and other information.

The following are examples of some commonly used Class object functions:

Class<? extends Person> clazz = person.getClass();

// 获取运行时类的名称(包含包名)
System.out.println(clazz.getName()); 

// 获取运行时类的简单名称(不包含包名)
System.out.println(clazz.getSimpleName());

// 获取运行时类的修饰符
int modifiers = clazz.getModifiers();
System.out.println(Modifier.toString(modifiers));

// 获取运行时类的父类
Class<?> superClass = clazz.getSuperclass();
System.out.println(superClass.getName());

// 获取运行时类实现的接口
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> iface : interfaces) {
    System.out.println(iface.getName());
}

// 获取运行时类的构造函数
Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor<?> constructor : constructors) {
    System.out.println(constructor.getName());
}

// 获取运行时类的字段
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
    System.out.println(field.getName());
}

In short, Java's getClass() function is a very useful function, which allows us to obtain the running status of the object at runtime. class, and obtain various information about the class through the Class object. It is very helpful for writing general code, performing reflection operations, and debugging code. I hope the introduction and examples in this article can help readers better understand and use the getClass() function.

The above is the detailed content of Java uses the getClass() function of the Object class to obtain the runtime class of the object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn