Home  >  Article  >  Java  >  Introduction to the method of obtaining a class in java

Introduction to the method of obtaining a class in java

零下一度
零下一度Original
2017-06-30 10:33:551560browse

Get all public methods of the class including the public methods of the parent class through the class.getMethos() method.

 1 import lombok.Data; 2  3 /** 4  * Created by hunt on 2017/6/27. 5  
 * 测试的实体类 6  * @Data 编译后会自动生成set、get、无惨构造、equals、canEqual、hashCode、toString方法 7  
 */ 8 @Data 9 public class Person {10     private String name;11     public String nickName;12     private int age;13     private void say(){14         System.out.println("say hunt");15     }16 }
View Code
 1 import java.lang.reflect.Method; 2  3 /** 4  * Created by hunt on 2017/6/27. 5  
 */ 6 public class NewInstanceTest { 7     public static void main(String[] args) { 8         
 Class<Person> personClass = Person.class; 9         try {10             
 Method[] methods = personClass.getMethods();11             for (Method m:methods) {12                 System.out.println(m);13             }14         } catch (Exception e) {15             e.printStackTrace();16         }17     }18 }
View Code

Get all properties of the class (public, protected, default, and private methods) through the class.getDeclaredMethods() method.

 1 import java.lang.reflect.Method; 2  3 /** 4  * Created by hunt on 2017/6/27. 5  
 */ 6 public class NewInstanceTest { 7     public static void main(String[] args) { 8         
 Class<Person> personClass = Person.class; 9         try {10             
 Method[] methods = personClass.getDeclaredMethods();11             for (Method m:methods) {12                 
 System.out.println(m);13             }14         } catch (Exception e) {15             
 e.printStackTrace();16         }17     }18 }
View Code

Note: The elements in the returned method array are not sorted, nor in any particular order.

Get the specific method:

 1 import java.lang.reflect.Method; 2  3 /** 4  * Created by hunt on 2017/6/27. 5  */ 6 public class NewInstanceTest { 7     public static void main(String[] args) { 8         Class<Person> personClass = Person.class; 9         try {10             Person p = personClass.newInstance();11             Method method = personClass.getMethod("setName", String.class);12             method.invoke(p,"hunt");13             System.out.println(p);14             method = personClass.getMethod("getName");15             System.out.println("getName方法"+method.invoke(p));16             method = personClass.getDeclaredMethod("say");17             method.setAccessible(true);//私有方法要授权18             method.invoke(p);19         } catch (Exception e) {20             e.printStackTrace();21         }22     }23 }
View Code

Summary: personClass.getDeclaredMethod("say");

Method.setAccessible(true);

Based on the protection of attributes, the default is false. This is equivalent to an authorization process, setting true to allow the operation of this attribute.

The above is the detailed content of Introduction to the method of obtaining a class in java. 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