子共有親のデータ
2584 ワード
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TeacherAnswer extends StudentPaper {
private String teacherAnswer;//
public String getTeacherAnswer() {
return teacherAnswer;
}
public void setTeacherAnswer(String teacherAnswer) {
this.teacherAnswer = teacherAnswer;
}
public void fatherToChild (Object father,Object child)throws Exception{
if(!(child.getClass().getSuperclass()==father.getClass())){
throw new Exception("child father ");
}
Class fatherClass= father.getClass();
Field ff[]= fatherClass.getDeclaredFields();
for(int i=0;i<ff.length;i++){
Field f=ff[i];// , deleteDate
Class type=f.getType();
Method m=fatherClass.getMethod("get"+upperHeadChar(f.getName()));// getDeleteDate
Object obj=m.invoke(father);//
f.set(child,obj);
}
}
private String upperHeadChar(String in){
String head=in.substring(0,1);
String out=head.toUpperCase()+in.substring(1,in.length());
return out;
}
}
public class StudentPaper {
// member protected private
protected String studentanswer;
public String getStudentanswer() {
return studentanswer;
}
public void setStudentanswer(String studentanswer) {
this.studentanswer = studentanswer;
}
}
public class StudentPaper {
private String studentanswer;
public String getStudentanswer() {
return studentanswer;
}
public void setStudentanswer(String studentanswer) {
this.studentanswer = studentanswer;
}
}
public static void main(String[] args) throws Exception {
StudentPaper s = new StudentPaper();
s.setStudentanswer(" ");
Answer a = new Answer();
a.fatherToChild(s,a);
System.out.println(a.toString());
}