填空题:利用反射,为对象的name属性赋值。
利用反射,为对象的name属性赋值。
class Student{
private String name;
private int age;
public String toString(){
return "name is" + name +",age is" + "age";
}
}
import java.lang.reflect.*;
public class Main {
public static void main(String[] args) {
Student p = new Student();
Class cla = @@[Student.class|p.getClass()|Class.forName("Student")](2);
Field nameField = cla.getDeclaredField("name");
nameField.setAccessible(true);
@@[nameField.set(p, "Jack")](2);//设置对象的姓名属性为Jack
}
}
答案:
第1空:Student.class || p.getClass() || Class.forName("Student") ||
第2空:nameField.set(p, "Jack") ||
class Student{
private String name;
private int age;
public String toString(){
return "name is" + name +",age is" + "age";
}
}
import java.lang.reflect.*;
public class Main {
public static void main(String[] args) {
Student p = new Student();
Class cla = @@[Student.class|p.getClass()|Class.forName("Student")](2);
Field nameField = cla.getDeclaredField("name");
nameField.setAccessible(true);
@@[nameField.set(p, "Jack")](2);//设置对象的姓名属性为Jack
}
}
答案:
第1空:Student.class || p.getClass() || Class.forName("Student") ||
第2空:nameField.set(p, "Jack") ||