题目拿百度翻译过了,就是让你在主类中实例化myStudent
我个人分析:题目,留了两个空白方法使用,应该是尽量不去修改,或者增加其他方法吧;
且成员变量是非静态的,还的让你在静态方法中去使用他,我添加了一个内部私有接口!
大致这样:
public class StudentProject {
private Student myStudent;
public static void main(String[] args) {
StudentProject sp=new StudentProject();
sp.myStudent=sp.Project();
System.out.println(sp.myStudent);
sp.myStudent.set("张三", 20);
System.out.println(sp.myStudent);
}
public Student Project() {
return new Student() {
private String name;
private int age;
public void set(String name,int age){
this.name=name;
this.age=age;
}
private Student get() {
return this;
}
public String toString() {
return "name:"+this.name+";age:"+this.age;
}
}.get();
}
private interface Student{
void set(String name,int age);
}
}
运行结果:
name:null;age:0
name:张三;age:20