标签:gif onclick style 技术 red eth ons isp methods
获取类的字节码的Class对象。
以Person类为例:
public class Person { private String name; private int age; public String a; protected int b; int c; private int d; public Person(String name, int age) { this.name = name; this.age = age; } public Person() { } 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; } public void eat(String str){ System.out.println(str); } @Override public String toString() { return "Person{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", a=‘" + a + ‘\‘‘ + ", b=" + b + ", c=" + c + ", d=" + d + ‘}‘; } }
1.格式:Class 类名称+Class(如personClass)=类名称.class.
例:Class personClass=Person.class.
2.获取类的构造函数。
例:Constructor constructor =personClass.getConstructor(String.class,int.class)
创建对象:Objct ob=constructor.newInstance("张三",25).
例:Constructor constructor =personClass.getConstructor( )
创建对象方法1:Objct ob=personClass.newInstance( ).
创建对象方法2:Objct ob=constructor.newInstance( ).
3.获取类的成员变量。
修改成员变量例如:
Person p=new Person( );
Field name=personClass.getFields("name" )
name.set(p,"张三");
修改成员变量例如:
Person p=new Person( );
Field name=personClass.getFields("name" );
name.setAccessible(true);//忽视修饰符
name.set(p,"张三");
4.获取类的成员方法。
使用方法:Method eat=personClass.getMethod(eat);
Person p=new Person( );
eat.invoke(p);
使用方法:Method eat=personClass.getMethod(eat,String.class);
Person p=new Person( );
eat.invoke(p."吃饭");
标签:gif onclick style 技术 red eth ons isp methods
原文地址:https://www.cnblogs.com/morefreedomofselfdiscipine/p/12236003.html