回应面向对象部分的强调:“类中的属性必须封装,封装后的属性要通过setter和getter方法设置和取得”。那么在使用反射机制进行调用方法操作时, 最重要的调用类中的setter和getter方法。
调用类中的setter和getter方法
调用类中的setter和getter方法
package zz.person;
class Person {
private String name;
private int age;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public String toString(){
return "姓名:" + this.name + "\n年龄:" + this.age;
}
}
package zz.invokesetgetdemo;
import java.lang.reflect.Method;
public class InvokeSetGetDemo{
public static void main(String []args){
Class<?> c = null;
Object obj = null;
try{
c = Class.forName("zz.Person");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
try {
obj = c.newInstance();
}catch (InstantiationException e){
e.printStackTrace();
}catch (IllegalAccessException e){
e.printStackTrace();
}
setter(obj, "name", "张泽", String.class);
setter(obj, "age", 18, int.class);
System.out.print("姓名:");
getter(obj, "name");
System.out.print("年龄:");
getter(obj, "age");
}
/*
*@param obj 操作的对象
*@param att 操作的属性
*@param value 设置的值
*@param type 参数的类型
*/
public static void setter(Object obj, String att, Object value, Class<?>type){
try {
Method met = obj.getClass().
getMethod("set" + initStr(att), type);
met.invoke(obj, value);
}catch (Exception e){
e.printStackTrace();
}
}
public static void getter(Object obj, String att){
try {
Method met = obj.getClass().getMethod("get" + initStr(att));
System.out.println(met.invoke(obj));
}catch (Exception e){
e.printStackTrace();
}
}
public static String initStr(String old){ // 将单词的首字母大写
String str = old.substring(0,1).toUpperCase() + old.substring(1) ;
return str ;
}
}
姓名:张泽
年龄:18
package zz.person;
class Person {
private String name;
private int age;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public String toString(){
return "姓名:" + this.name + "\n年龄:" + this.age;
}
}
package zz.invokesetgetdemo;
import java.lang.reflect.Method;
public class InvokeSetGetDemo{
public static void main(String []args){
Class<?> c = null;
Object obj = null;
try{
c = Class.forName("zz.Person");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
try {
obj = c.newInstance();
}catch (InstantiationException e){
e.printStackTrace();
}catch (IllegalAccessException e){
e.printStackTrace();
}
setter(obj, "name", "张泽", String.class);
setter(obj, "age", 18, int.class);
System.out.print("姓名:");
getter(obj, "name");
System.out.print("年龄:");
getter(obj, "age");
}
/*
*@param obj 操作的对象
*@param att 操作的属性
*@param value 设置的值
*@param type 参数的类型
*/
public static void setter(Object obj, String att, Object value, Class<?>type){
try {
Method met = obj.getClass().
getMethod("set" + initStr(att), type);
met.invoke(obj, value);
}catch (Exception e){
e.printStackTrace();
}
}
public static void getter(Object obj, String att){
try {
Method met = obj.getClass().getMethod("get" + initStr(att));
System.out.println(met.invoke(obj));
}catch (Exception e){
e.printStackTrace();
}
}
public static String initStr(String old){ // 将单词的首字母大写
String str = old.substring(0,1).toUpperCase() + old.substring(1) ;
return str ;
}
}