java 学习之路----java开发实战经典反射习题答案(15章)

java 学习之路----java开发实战经典反射习题答案(15章)_第1张图片


答案:


class Student{
private String name;
private String age;
private String score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public Student() {
}


}


public class FSTest {
public static void main(String[] args)throws Exception {
DataOutputStream out=new DataOutputStream(new FileOutputStream(new File("f:"+File.separator+"a.txt")));
DataInputStream in=new  DataInputStream(new FileInputStream(new File("f:"+File.separator+"a.txt")));
System.out.println("请输入学生信息:(like:20:100)");
boolean Y=true;
String str = "";
Scanner scanner=new Scanner(System.in);
while(Y){


str+= scanner.next()+":";



System.out.println("是否继续输入:是:Y:否:N");
String a= scanner.next();
if(a.equalsIgnoreCase("Y")){
Y=true;
}
if(a.equalsIgnoreCase("N")){
scanner.close();
Y=false;
}
}
out.writeBytes(str);
int temp=0,len=0;
byte b[]=new byte[1024];
while((temp=in.read())!=-1){
b[len]=(byte) temp;
len++;

}
in.close();
out.close();
String s=new String(b);

String string[]=s.split("\\:");
Class student=(Class) Class.forName("FS.Student");
int j=0;
Student student2=null;
Listlist=new ArrayList<>();
for(int i=0;i
if((i+1)%3==1){
student2=student.newInstance();
student2.setName(string[i]);
}else if((i+1)%3==2){


student2.setAge(string[i]);
}else if((i+1)%3==0){
System.out.println(string[i]);

student2.setScore(string[i]);
list.add(student2);
}
}

for(Student s2:list){
System.out.println("姓名:"+s2.getName()+"  年龄:"+s2.getAge()+"  成绩:"+s2.getScore());
}

}
}

你可能感兴趣的:(javase)