【简单问题】关于uper()

小辉 2008-09-11 09:23:44
代码虽然长,但是非常简单,呵呵。各位大虾,请帮忙。

想实现这个:
父类构造体中接受传过来的字符串数组,
根据数组中的属性名 ,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());

}
...全文
215 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT小岛 2008-09-11
  • 打赏
  • 举报
回复
我做啦一下修改可以满足你的要求 你把 strName strAge 以参数传到父类 子类是不会受到影响的
IT小岛 2008-09-11
  • 打赏
  • 举报
回复
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());

}
}
wangydong 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dracularking 的回复:]
子类属性初始化会在父类构造方法后

将子类属性转移给父类,配上合适的访问控制符,是解决此问题的方法之一
[/Quote]
justinavril 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 crazy_boy1 的帖子:]
子类中,先执行了super()
然后 执行了这句:
private String strName=null;
private String strAge=null;
把设置的值给清空了。
[/Quote]
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!
justinavril 2008-09-11
  • 打赏
  • 举报
回复
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;
把设置的值给清空了。 "
renmms 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 justinavril 的回复:]
引用 6 楼 renmms 的回复:
引用 5 楼 justinavril 的回复:
引用 4 楼 renmms 的回复:
新建个构造方法,加上子类的参数呢?


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


what's your meaning of reflection?

反射

Sorry, I can not type Chinese in company, because I have no Chinese Environment and no admin privilege to my PC.
[/Quote]

o,I see.My os is also EN Vista,English Environment too,but I hava installed "sougou piyin",it is convenience.
:)
justinavril 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 renmms 的回复:]
引用 5 楼 justinavril 的回复:
引用 4 楼 renmms 的回复:
新建个构造方法,加上子类的参数呢?


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


what's your meaning of reflection?
[/Quote]
反射

Sorry, I can not type Chinese in company, because I have no Chinese Environment and no admin privilege to my PC.
renmms 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 justinavril 的回复:]
引用 4 楼 renmms 的回复:
新建个构造方法,加上子类的参数呢?


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

what's your meaning of reflection?
justinavril 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 renmms 的回复:]
新建个构造方法,加上子类的参数呢?
[/Quote]

Of course, it can meet the demands. But maybe LZ just want to achieve it through reflection.
renmms 2008-09-11
  • 打赏
  • 举报
回复
新建个构造方法,加上子类的参数呢?
小辉 2008-09-11
  • 打赏
  • 举报
回复
TO:dracularking

>>将子类属性转移给父类,配上合适的访问控制符,是解决此问题的方法之一

呵呵,你说的没太理解,具体怎么搞啊,能说说吗,多谢。
justinavril 2008-09-11
  • 打赏
  • 举报
回复
strMedName.equals("") is not proper.


"".equals(strMedName)
dracularking 2008-09-11
  • 打赏
  • 举报
回复
子类属性初始化会在父类构造方法后

将子类属性转移给父类,配上合适的访问控制符,是解决此问题的方法之一
jingulang 2008-09-11
  • 打赏
  • 举报
回复
好长

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧