Java之Class字节码对象获取一个类中的字段值、公共构造方法、私有构造方法、创建对象、以及所有方法

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 09:35   104   0

Class字节码对象

1.获取公共构造方法私有构造方法 getConstructor() getDeclaredConstructor()
2.获取构造方法创建对象 newInstance()
3.获取一个类里面所有的方法 getMethods()
4.获取一个类里面的字段值 getField()

ex:

/**
 * Class字节码对象
 * 1.获取公共构造方法、私有构造方法      getConstructor()      getDeclaredConstructor()
 * 2.获取构造方法创建对象                        newInstance()
 * 3.获取一个类里面所有的方法                 getMethods()
 * 4.获取一个类里面的字段值                     getField()
 * @author 郑清
 */
public class Demo {
 
 public static void main(String[] args) throws Exception {
  testClassGetConstructor();//字节码对象获取公共构造方法、私有构造方法
  testClassGetObject();//字节码对象获取构造方法创建对象
  testClassMethod();//字节码对象获取一个类里面所有的方法(包含这个类默认继承的父类Object里面的所有公共(public)的方法)
  testField();//字节码对象获取一个类里面的字段值
 }
 
 public static void testClassGetConstructor() throws Exception{
  System.out.println("========================字节码对象获取公共构造方法、私有构造方法=========================");
  //获取类的字节码对象
  Class cla = Student.class;
  Constructor constructor = cla.getConstructor(String.class);//根据小括号中传入的字节码对象 获得指定参数类型(这里是:String)的构造方法
  System.out.println(constructor);
  
  //获取所有公共(public)的构造方法    然后遍历
  Constructor[] constructors = cla.getConstructors();
  for (Constructor constructor2 : constructors) {
   System.out.println(constructor2);
  }
  
  //获取私有(private)的构造方法
  Constructor declaredConstructor = cla.getDeclaredConstructor(int.class);
  System.out.println(declaredConstructor);
  
  //如果类中不存在的东西,用反射都是可以获取的,但是仅仅是编译不会报错,运行还是会报错
  //Constructor declaredConstructor2 = cla.getDeclaredConstructor(double.class);
  //System.out.println(declaredConstructor2);//NoSuchMethodException
 }
 
 public static void testClassGetObject() throws Exception{
  System.out.println("==========================字节码对象获取构造方法创建对象============================");
  //获取类的字节码对象
  Class cla = Student.class;
  //获取公共(public)的构造方法:
  Constructor constructor = cla.getConstructor(String.class,int.class);
  //创建对象    注意:上面获得的是带2个参数的构造方法==>所以这里使用获取的构造方法的时候 也必须写对应的参数     如果不写编译没问题,但运行会报错
  Object newInstance = constructor.newInstance("String类型name",18);
  System.out.println("newInstance:"+newInstance);
  
  //获取私有(private)的构造方法
  Constructor declaredConstructor = cla.getDeclaredConstructor(int.class);
  //能获取但不能使用 ==》因为还是会检查权限
  declaredConstructor.setAccessible(true);//忽略权限检查
  //创建对象
  Object newInstance2 = declaredConstructor.newInstance(18);
  System.out.println("newInstance2:"+newInstance2);
  System.out.println(newInstance2 instanceof Student);//true
 }
 
 public static void testClassMethod() throws Exception{
  System.out.println("========字节码对象获取一个类里面所有的方法(包含继承的父类里面的所有公共(public)的方法)=========");
  //获取类的字节码对象
  Class cla = Student.class;
  //获取无参公共(public)的构造方法
  Constructor constructor = cla.getConstructor();
  //创建对象
  Object newInstance = constructor.newInstance();
  
  //获取Student类里面所有公共(public)的方法(包括默认继承父类Object里面的所有公共(public)的方法)  
  Method[] methods = cla.getMethods();
  for (Method method : methods) {
   System.out.println(method);
  }
  
  //表示得到了一个方法,而且是cla所表示的类中的方法,对象方法
  Method method = cla.getMethod("add", String.class);
  method.invoke(newInstance, "1");
 }
 
 public static void testField() throws Exception{
  System.out.println("======================字节码对象获取一个类里面的字段值=========================");
  //获取类的字节码对象
  Class cla = Student.class;
  
  //获取无参公共(public)的构造方法 创建对象
  Object obj = cla.getConstructor().newInstance();
  //System.out.println(obj);//Student对象
  
  //获取字段name
  Field field = cla.getField("name");
  
  //设置字段值        注意:字段值需要设置到某个对象上面去
  field.set(obj, "String类型name值");
  System.out.println(field.get(obj));
  
  Student stu = (Student)obj;
  System.out.println(stu.name);
 }
 
}
class Student {
 
 public String name;
 
 public Student() {}
 
 public Student(String name) {}
 
 public Student(String name,int age) {}
 
 private Student(int age) {}
 
 public void add() {}
 
 public void add(String name) {
  System.out.println("这是add方法...");
 }
 
 private void add(String name,int age) {}
 
}

运行结果图:

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP