您的位置:首页 > 其它

抽象类的实例化

2015-12-10 10:18 197 查看
eg:

public abstract class Base {

public Base(int i){
System.out.println("Base constructor.i="+i);
}
public abstract void f();
}

public class AnonymousConstructor {
public static Base getBase(int i){
return new Base(i){
public void f(){
System.out.println("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();

[error:

Base b = new Base(2);//Cannot instantiate the type Base]
}

}

result:

Base constructor.i=47

In anonymous f()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: