3. 下面代码正确吗?如有错,请改正
public interface A{
void test();
}public A getA(String s) {
class AImpl implements A{
private String b;
private AImpl() {
b = s;
}
public String getB() {
return b;
}
}
return new AImpl();
}
public interface A{
void test();
}public A getA(String s) {
class AImpl implements A{
private String b;
private AImpl() {
b = s;
}
public String getB() {
return b;
}
}
return new AImpl();
}
类AImpl必须实现test()方法.
void test();
} public A getA(final String s) {
class AImpl implements A{
private String b;
private AImpl() {
b =s;
}
public String getB() {
return b;
}
public void test() {
}
}
return new AImpl();
}
package others;interface A{
void test();
}public class Test{
public A getA(final String s) {
class AImp implements A{
private String b;
private AImp() {
b =s;
//System.out.println(this.b);
}
public String getB() {
return b;
}
public void test() {
//System.out.println("AImp's Test");
} }
return new AImp();
}
public static void main(String[] args){
A atest=new Test().getA("test AImp()");
atest.test();
}
}
2 method getA()不再具体的类中。
3 AImp 的构造函数为private 不能创建AImp的对象,必须改为public
2 类AImpl的构造函数应该是public类型
3 类AImpl必须实现接口的test()方法