I have a Parent.class(User and Element can be replaced by any classes)
public abstract class Parent {
User user;
public void InitUser() {
Fileds f = this.getClass().getDeclaredFields();
for(int i=0; i<f.length; i++) {
f[i].set(this, new Element(i));
}
}
}
Child.class
public class Child extends Parent {
Element e1;
Element e2;
public Child(User user) {
super();
this.user = user;
}
public void doSomethingUsingE1AndE2() {
// do something by using e1 and e2, invoking user's methods.
user.perform(e1);
user.perform(e2);
}
}
In my Client.class, is there any differences between the following two ways to construct and use Child?
1.
public class Client() {
User user = new User();
Class childClass = Class.forName("com.xxx.Child");
Constructor con = childClass .getConstructor(User.class);
Object o = con.newInstance(user);
Method initMethod = child.getSuperclass().getDeclaredMethod(
"InitUser");
initMethod.invoke(o);
Method keyMethod = child.getMethod("doSomethingUsingE1AndE2");
keyMethod.invoke(o);
}
2.
public class Client() {
User user = new User();
Child c = new Child(user);
c.InitUser();
c.doSomethingUsingE1AndE2();
}
Thanks a lot.
Aucun commentaire:
Enregistrer un commentaire