Itest 是一个接口(interface)。在这里我提到了像 new Itest()。那么这是否意味着我可以为接口(interface)创建对象?
public interface Itest {
}
static final Itest s = new Itest(){
};
就像,我们可以为接口(interface)创建对象,而无需任何类实现该接口(interface)。
最佳答案
听起来您正在寻找的是 anonymous classes .
它们最常用的是这样的:
interface Something { void execute(); }
// In some code...
setSomething(new Something() {
public void execute() {
// Your code here
}
});
关于java - 是否可以为接口(interface)创建对象而不用java中的任何类实现它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432044/