In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Object.getClass () in Java". In daily operation, I believe many people have doubts about how to use Object.getClass () in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Object.getClass () in Java". Next, please follow the editor to study!
Java Object.getClass () method
Object.getClass () method, the return value of this method is Class type, Class c = obj.getClass (); through object c, we can get all member methods of the object, each member method is a Method object; we can also get all member variables of the object, each member variable is a Field object; similarly, we can also get the object's constructor, which is a Constructor object
For more information, see the following example package classTest; import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method / * print the information of the class, including the constructor and member function of the class The member variable * @ author Wang * * / public class ClassUtil {/ * gets information about the member methods of an object * * @ param obj * / public static void printClassMethodMessage (Object obj) {/ / to get information about a class, you must first get the class type of the class, which subclass's object c is the subclass's class type Class c = class () / / get the name of the class System.out.println ("the name of the class is:" + c.getName ()) / * * Method class, method object A member method is a Method object * the getMethods () method gets all the public functions, including the parent class inherited * getDeclaredMethods () gets all the methods declared by the class itself, regardless of access rights * / / c.getDeclaredMethods () Method [] ms = c.getMethods () For (int I = 0; I
< ms.length; i++) { // 得到方法的返回值类型的类类型 Class returnType = ms[i].getReturnType(); System.out.print(returnType.getName() + " "); // 得到方法的名称 System.out.print(ms[i].getName() + "("); // 获取参数类型--->What you get is the class type of the parameter list type Class [] paramTypes = Ms [I] .getParameterTypes (); for (Class class1: paramTypes) {System.out.print (class1.getName () + ",");} System.out.println (")") }} / * get the information of the object's member variables * * @ param obj * / public static void printFieldMessage (Object obj) {Class c = obj.getClass () / * * member variables are also objects the java.lang.reflect.Field Field class encapsulates operations on member variables * the getFields () method acquires information about all member variables of public * getDeclaredFields acquires information about member variables declared by the class itself * / / Field [] fs = c.getFields () Field [] fs = c.getDeclaredFields (); for (Field field: fs) {/ / get the class type of the member variable Class fieldType = field.getType (); String typeName = fieldType.getName (); / / get the name of the member variable String fieldName = field.getName () System.out.println (typeName + "" + fieldName);}} / * * print the information of the object's constructor * * @ param obj * / public static void printConMessage (Object obj) {Class c = obj.getClass (); / * * the constructor is also the object java.lang. Constructor encapsulates constructor information * getConstructors gets all public constructors getDeclaredConstructors gets all constructors * / Constructor [] cs = c.getConstructors (); Constructor [] cs = c.getDeclaredConstructors (); for (Constructor constructor: cs) {System.out.print (constructor.getName () + "(")) / / get the parameter list of the constructor-> get the class type of the parameter list Class [] paramTypes = constructor.getParameterTypes (); for (Class class1: paramTypes) {System.out.print (class1.getName () + ",");} System.out.println (")") } Java Object.getClass () source code analysis
1. Stack
Person object reference
2. Heap
Person instance object, Person.class information
Person instance object:
Mark word: object header, which stores instance summary information, such as lock information, hash value, thread information, etc.
Kclass metadata type pointer, pointer to Person type metadata
Instance data: the values of various member variables of the object.
Person.class information
Person.class class information, method, field information.
3. Person type metadata of the old generation.
Mark word: type headers, such as human lock information
Kclass metadata type pointer: type pointer to Person type
Mirror information for the java mirror:Person.class class.
Reflection mechanism
Person object pointer, get the Person object store first
Get the Person type metadata according to the kclass metadata type pointer of the Person object
Get the information of the Person.class class from the java mirror in the Person type metadata
At this point, the study on "how to use Object.getClass () in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.