反射在一定程度上破坏了封装性,需要合理使用
Student类 extends Person类
两个类的代码在文章最下方
获取成员变量,包括子类及父类,同时只能包含公共(public)属性
Class<?> clazz = Class.forName("com.mashibing.reflect.Student");
//获取成员变量,包括子类及父类,同时只能包含公共的方法
Field[] fields = clazz.getFields();
for (Field field : fields) {
System.out.println(field);
System.out.println(field.getName());
System.out.println(field.getType());
System.out.println(field.getModifiers());
System.out.println("-----");
}
此方法返回的是当前类的所有属性,不仅仅局限于公共访问修饰符,所有的访问修饰符都可以拿到
Class<?> clazz = Class.forName("com.mashibing.reflect.Student");
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
获取该对象的普通方法,包含的方法范围是当前对象及父类对象的所有公共方法
Class.forName("com.mashibing.reflect.Student");
Method[] methods = clazz.getMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
获取当前类中所有的方法,无论什么访问修饰符
Class.forName("com.mashibing.reflect.Student");
Method[] methods = clazz.getMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
一定要加address.setAccessible(true),设置该属性是否能被访问
Class.forName("com.mashibing.reflect.Student");
Field address = clazz.getDeclaredField("address");
//设置该属性是否能被访问,true表示能被访问,破坏了封装性
address.setAccessible(true);
System.out.println(address.getName());
//newInstance默认会调用类的无参构造方法
// 以后在使用框架的时候,类一定记得加无参构造方法,因为其底层可能就会调用
Object o = clazz.newInstance();
address.set(o,"北京市");
System.out.println(((Student)o).getAddress());
一定要加address.setAccessible(true),设置该属性是否能被访问
Class.forName("com.mashibing.reflect.Student");
Method add = clazz.getDeclaredMethod("add", int.class, int.class);
add.setAccessible(true);
Object o1 = clazz.newInstance();
add.invoke(o1,123,123);
获取对象公有的构造方法,且不包括父类的构造方法
Class.forName("com.mashibing.reflect.Student");
Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor.getName());
}
获取所有的构造方法,无论是私有还是公有
Class.forName("com.mashibing.reflect.Student");
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor.getName());
}
Class.forName("com.mashibing.reflect.Student");
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class, int.class, String.class);
declaredConstructor.setAccessible(true);
Student o2 = (Student)declaredConstructor.newInstance("msb", 23, "java");
System.out.println(o2);
public class Student extends Person{
public String className;
private String address;
public Student(){
super();
}
private Student(String name,int age,String className){
super(name,age);
this.className= className;
}
public Student(String name,int age,String className,String address){
super(name,age);
this.className = className;
this.address = address;
}
private void add(int a,int b){
System.out.println(a+b);
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"className='" + className + '\'' +
", address='" + address + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Person {
public String name;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String show(){
return "name:"+name+", age :"+age;
}
}