反射

上一篇 / 下一篇  2017-08-24 09:37:08

package com.pb.reflect.classinfo;

public class Person {
private String name;
private String gender;
private int age;
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return "姓名是:"+name+"/n年龄:"+age;
}
}


package com.pb.reflect.classinfo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
//通过.class获取类的属性和方法
public class TestReflection22 {

public static void main(String[] args) {
Class cla=Person11.class;
System.out.println("获取类的方法");
Method[] meth=cla.getDeclaredMethods();
for(Method method:meth){
System.out.println(method.toString());
}
System.out.println("获取类的属性");
Field[] fields=cla.getDeclaredFields();
for(Field  field:fields){
System.out.println(field.toString());
}
}

}

package com.pb.reflect.classinfo;
@Deprecated
public class RefelectionDemo33 {
private RefelectionDemo33(){}
public RefelectionDemo33(String name){}
public  void info(){}
public  void info(String str){}
class inner{}
}

package com.pb.reflect.classinfo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
//获取类的具体信息 
public class TestRefelectionDemo33 {
public static void main(String[] args) {
Class cla=RefelectionDemo33.class;
Constructor[] cons=cla.getDeclaredConstructors();
System.out.println("获取 全部构造方法 ");
for(Constructor con:cons){
System.out.println(con.toString());
}
Constructor[] constructor=cla.getConstructors();
System.out.println("获取public构造方法");
for(Constructor c:constructor){
System.out.println(c.toString());
}
Method[] methods=cla.getDeclaredMethods();
System.out.println("获取全部方法");
for(Method m:methods){
System.out.println(m.toString());
}
try {
Method method=cla.getMethod("info", String.class);
System.out.println("获取指定info方法,为String类型的参数");
System.out.println(method.toString());
Method m=cla.getMethod("info", null);
System.out.println("获取指定info方法,无参");
System.out.println(m.toString());
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Annotation[] anno=cla.getDeclaredAnnotations();
System.out.println("获取全部注释");
for(Annotation a:anno){
System.out.println(a.toString());
}
Package packages=cla.getPackage();
System.out.println("获取包");
System.out.println(packages.toString());
Class[] c=cla.getDeclaredClasses();
System.out.println("获取内部类");
for(Class  classes:c){
System.out.println(classes.toString());
try {
Class inner=Class.forName("com.pb.reflect.classinfo.RefelectionDemo33$inner");
System.out.println("内部类的具体信息:"+inner.getDeclaringClass());
System.out.println("内部类的包:"+inner.getPackage());
System.out.println("内部类的父类:"+inner.getSuperclass());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2017-09-05  
     12
3456789
10111213141516
17181920212223
24252627282930

我的存档

数据统计

  • 访问量: 147
  • 日志数: 2
  • 建立时间: 2017-08-23
  • 更新时间: 2017-08-24

RSS订阅

Open Toolbar