正在加载图片...
class student public string name; public int age; public Student(String name, int age) this name=name Sage=age, Student stu0=new Student(Zhangsan, 21); Field age Field= Student class get Field(age"); int age Value= ageField getInt(stu0); age Field. setInt(25) System. out. printIn(stuO age); 25java.lang.reflect.Field Class • 类Field用于表示类的域 • 该类声明了以下方法用于获取域的类型 public Class getType() • 该类声明了以下方法用于获取域的值 public <Type> get<Type>(Object obj) • 该类声明了以下方法用于设置域的值 public void set<Type>(<Type> value) class Student{ public String name; public int age; public Student(String name, int age){ this.name=name; this.age=age; } } Student stu0 = new Student(“Zhangsan”, 21); Field ageField = Student.class.getField(“age”); int ageValue = ageField.getInt(stu0); ageField.setInt(25); System.out.println(stu0.age); 25
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有