概述
因为演示需要,所以把Person类尽量的造得尽可能的复杂,如下
Person类:
@MyAnnotation(value = "PeterLi") public class Person extends Creature<String> implements Comparable, MyInterface { public String name; private int age; int id; protected boolean sex; //创建类时,尽量保留一个空参的构造器 public Person() { // System.out.println("今天天气很闷热"); } public Person(String name) { this.name = name; } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @MyAnnotation(value = "abc123") public void show() { System.out.println("我是一个人!"); } private void display(String nation) throws Exception{ System.out.println("我的国籍是" + nation); } @Override public String toString() { return "Person{" + "name='" + name + ''' + ", age=" + age + '}'; } @Override public int compareTo(Object o) { return 0; } class Bird{ } }
Creature父类:
public class Creature<T> { public double weight; public void breath() { System.out.println("呼吸"); } }
MyInterface接口:
public interface MyInterface extends Serializable { }
MyAnnotation注解:
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value(); }
下面是代码示例:
public class TestMethod { //1.获取运行时类的方法 @Test public void test1() { Class clazz = Person.class; //1.getMethods():获取运行时类及其父类中所有声明为public的方法 Method[] m1 = clazz.getMethods(); for (Method m : m1) { System.out.println(m); } System.out.println(); //2.getDeclaredMethods():获取运行时类本身声明的所有方法 Method[] m2 = clazz.getDeclaredMethods(); for (Method m : m2) { System.out.println(m); } } //注解 权限修饰符 返回值类型 方法名 形参列表 异常(要抛出来的才能看到) @Test public void test2() { Class clazz = Person.class; Method[] m2 = clazz.getDeclaredMethods(); for (Method m : m2) { Annotation[] ann = m.getAnnotations(); //1.注解 for (Annotation a : ann) { System.out.print("注解:" + a + " "); } //2.权限修饰符 String str = Modifier.toString(m.getModifiers()); System.out.print("权限修饰符:" + str + " "); //3.返回值类型 Class returnType = m.getReturnType(); System.out.println("返回值类型:" + returnType.getName() + " "); //4.方法名 System.out.print("方法名:" + m.getName() + " "); //5.形参列表 System.out.print("形参列表:("); Class[] params = m.getParameterTypes(); for (int i = 0; i < params.length; i++) { System.out.print(params[i].getName() + " args-" + i + " "); } System.out.print(") "); //6.异常类型 Class[] exps = m.getExceptionTypes(); if (exps.length != 0){ System.out.print("异常:"); } for (int i = 0; i < exps.length; i++) { System.out.print(exps[i].getName() + " "); } System.out.println(); } } }
test1结果:
public java.lang.String com.peter.java19.Person.toString()
public int com.peter.java19.Person.compareTo(java.lang.Object)
public java.lang.String com.peter.java19.Person.getName()
public void com.peter.java19.Person.setName(java.lang.String)
public void com.peter.java19.Person.show()
public int com.peter.java19.Person.getAge()
public void com.peter.java19.Person.setAge(int)
public void com.peter.java19.Creature.breath()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()public java.lang.String com.peter.java19.Person.toString()
public int com.peter.java19.Person.compareTo(java.lang.Object)
public java.lang.String com.peter.java19.Person.getName()
public void com.peter.java19.Person.setName(java.lang.String)
public void com.peter.java19.Person.show()
public int com.peter.java19.Person.getAge()
public void com.peter.java19.Person.setAge(int)
private void com.peter.java19.Person.display(java.lang.String) throws java.lang.Exceptiontest2结果:
权限修饰符:public 返回值类型:java.lang.String
方法名:toString 形参列表:()
权限修饰符:public 返回值类型:int
方法名:compareTo 形参列表:(java.lang.Object args-0 )
权限修饰符:public 返回值类型:java.lang.String
方法名:getName 形参列表:()
权限修饰符:public 返回值类型:void
方法名:setName 形参列表:(java.lang.String args-0 )
权限修饰符:public 返回值类型:int
方法名:getAge 形参列表:()
权限修饰符:public 返回值类型:void
方法名:setAge 形参列表:(int args-0 )
注解:@com.peter.java19.MyAnnotation(value=abc123) 权限修饰符:public 返回值类型:void
方法名:show 形参列表:()
权限修饰符:private 返回值类型:void
方法名:display 形参列表:(java.lang.String args-0 ) 异常:java.lang.Exception
最后
以上就是多情洋葱为你收集整理的Java基础巩固系列 通过反射获取类的完整结构——Mothed(方法篇)的全部内容,希望文章能够帮你解决Java基础巩固系列 通过反射获取类的完整结构——Mothed(方法篇)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复