代码虽然长,但是非常简单,呵呵。各位大虾,请帮忙。想实现这个:
父类构造体中接受传过来的字符串数组,
根据数组中的属性名 ,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());

}

解决方案 »

  1.   

    strMedName.equals("")   is not proper.
    "".equals(strMedName)
      

  2.   

    TO:dracularking>>将子类属性转移给父类,配上合适的访问控制符,是解决此问题的方法之一呵呵,你说的没太理解,具体怎么搞啊,能说说吗,多谢。
      

  3.   


    Of course, it can meet the demands. But maybe LZ just want to achieve it through reflection.
      

  4.   


    what's your meaning of reflection?
      

  5.   

    反射   Sorry, I can not type Chinese in company, because I have no Chinese Environment and no admin privilege to my PC.
      

  6.   


    o,I see.My os is also EN Vista,English  Environment too,but I hava installed "sougou piyin",it is convenience.
    :)
      

  7.   

    Here is my opinion:In your class ClsSon, firstly strName and strAge are initialized, then the constructors run. So I am not agree with :"子类中,先执行了super() 
    然后 执行了这句: 
    private String strName=null; 
    private String strAge=null; 
    把设置的值给清空了。 "
      

  8.   

    Here is my opinion:In class ClsFather, firstly the variables: strName and strAge are initialized, then the constructor runs.So I don't agree with you.Maybe I made a mistake, but you need to check that the constructor of ClsFather works fine!
      

  9.   

    import java.lang.reflect.InvocationTargetException; 
    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()); }
    }
      

  10.   

    我做啦一下修改可以满足你的要求 你把 strName  strAge 以参数传到父类 子类是不会受到影响的