


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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.