java语言编程题,题目如下

2024-11-29 00:23:56
推荐回答(2个)
回答1:

package test;

public class Father {

private String name;

class Child{
public void introFather(){
System.out.println(name);
}
}

public Child newChild(){
return new Child();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
package test;

import test.Father.Child;

public class Test {

public static void main(String[] args) {
Father father= new Father();
father.setName("zhangjun0");
Child child=father.newChild();
child.introFather();
}

}

回答2:

public class Father {
private String name="zhangjun0";

class Child {
public void introFather(){
System.out.println(name);
}
}
}
---------------------------------
public class Test {
public static void main(String[] args) {
Father.Child child=new Father().new Child();
child.introFather();
}
}