代码虽然长,但是非常简单,呵呵。各位大虾,请帮忙。想实现这个:
父类构造体中接受传过来的字符串数组,
根据数组中的属性名 ,invoke对应的子类中的set方法,
初始化子类中的属性。显然这种方法不行
子类中,先执行了super()
然后 执行了这句:
private String strName=null;
private String strAge=null;
把设置的值给清空了。问题:
如何实现执行了父类的构造函数后,不执行子类的属性定义中的赋初值?
(这么设计父类的目的,是想在子类中继承后,能自动初始化属性值。)父类:
---------------------------------------------------
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public abstract class ClsFather {
/**
* 根据字符串数组中的子类的属性名称,执行invoke设置子类属性值。
* @param aryStr
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
public ClsFather(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { String strMedName = null; for (int i = 0; i < aryStr.length; i++) {
strMedName = aryStr[i].toString();
if (strMedName != null && !strMedName.equals("")) {
// 获得方法名
strMedName = strMedName.replaceFirst(
strMedName.substring(0, 1), strMedName.substring(0, 1).toUpperCase());
// invoke执行方法 setXXXXXX(String)
Method method = this.getClass().getMethod("set" + strMedName,new Class[] {String.class});
Object []obj = new Object[] {new String(strMedName)};
method.invoke(this, obj);
}
}
}
}
---------------------------------------------------
子类:
---------------------------------------------------
import java.lang.reflect.InvocationTargetException;
public class ClsSon extends ClsFather {
private String strName=null;
private String strAge=null; public ClsSon(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException {
super(aryStr);
} public String getStrAge() {
return strAge;
} public void setStrAge(String strAge) {
this.strAge = strAge;
} public String getStrName() {
return strName;
} public void setStrName(String strName) {
this.strName = strName;
}
}
---------------------------------------------------
//调用
public static void main(String[] args) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { ClsSon clsSon = new ClsSon (new String[] {"strName","strAge"});
System.out.println(clsSon.getStrAge());
System.out.println(clsSon.getStrName());
}
父类构造体中接受传过来的字符串数组,
根据数组中的属性名 ,invoke对应的子类中的set方法,
初始化子类中的属性。显然这种方法不行
子类中,先执行了super()
然后 执行了这句:
private String strName=null;
private String strAge=null;
把设置的值给清空了。问题:
如何实现执行了父类的构造函数后,不执行子类的属性定义中的赋初值?
(这么设计父类的目的,是想在子类中继承后,能自动初始化属性值。)父类:
---------------------------------------------------
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public abstract class ClsFather {
/**
* 根据字符串数组中的子类的属性名称,执行invoke设置子类属性值。
* @param aryStr
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
public ClsFather(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { String strMedName = null; for (int i = 0; i < aryStr.length; i++) {
strMedName = aryStr[i].toString();
if (strMedName != null && !strMedName.equals("")) {
// 获得方法名
strMedName = strMedName.replaceFirst(
strMedName.substring(0, 1), strMedName.substring(0, 1).toUpperCase());
// invoke执行方法 setXXXXXX(String)
Method method = this.getClass().getMethod("set" + strMedName,new Class[] {String.class});
Object []obj = new Object[] {new String(strMedName)};
method.invoke(this, obj);
}
}
}
}
---------------------------------------------------
子类:
---------------------------------------------------
import java.lang.reflect.InvocationTargetException;
public class ClsSon extends ClsFather {
private String strName=null;
private String strAge=null; public ClsSon(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException {
super(aryStr);
} public String getStrAge() {
return strAge;
} public void setStrAge(String strAge) {
this.strAge = strAge;
} public String getStrName() {
return strName;
} public void setStrName(String strName) {
this.strName = strName;
}
}
---------------------------------------------------
//调用
public static void main(String[] args) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { ClsSon clsSon = new ClsSon (new String[] {"strName","strAge"});
System.out.println(clsSon.getStrAge());
System.out.println(clsSon.getStrName());
}
"".equals(strMedName)
Of course, it can meet the demands. But maybe LZ just want to achieve it through reflection.
what's your meaning of reflection?
o,I see.My os is also EN Vista,English Environment too,but I hava installed "sougou piyin",it is convenience.
:)
然后 执行了这句:
private String strName=null;
private String strAge=null;
把设置的值给清空了。 "
import java.lang.reflect.Method; public abstract class ClsFather {
/**
* 根据字符串数组中的子类的属性名称,执行invoke设置子类属性值。
* @param aryStr
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
protected String[] strMedName = new String[2];
public ClsFather(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { for (int i = 0; i < aryStr.length; i++) {
strMedName[i] = aryStr[i];
if (strMedName != null && !strMedName.equals("")) {
// 获得方法名
strMedName[i] = strMedName[i].replaceFirst(
strMedName[i].substring(0, 1), strMedName[i].substring(0, 1).toUpperCase());
// invoke执行方法 setXXXXXX(String)
Method method = this.getClass().getMethod("set" + strMedName[i],new Class[] {String.class});
Object []obj = new Object[] {new String(strMedName[i])};
method.invoke(this, obj);
}
}
}
}
package new1;import java.lang.reflect.InvocationTargetException;
public class ClsSon extends ClsFather { private String strName=null;
private String strAge=null; public ClsSon(String[] aryStr) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { super(aryStr);
this.strAge = super.strMedName[0];
this.strName = super.strMedName[1];
} public String getStrAge() {
return strAge;
} public void setStrAge(String strAge) {
this.strAge = strAge;
} public String getStrName() {
return strName;
} public void setStrName(String strName) {
this.strName = strName;
} public static void main(String[] args) throws
NoSuchMethodException, IllegalAccessException, InvocationTargetException { ClsSon clsSon = new ClsSon (new String[] {"strName","strAge"}); System.out.println(clsSon.getStrAge());
System.out.println(clsSon.getStrName()); }
}