gpt4 book ai didi

java - 我在哪里可以找到有关此类隐式类实现的更多信息

转载 作者:行者123 更新时间:2023-11-30 10:32:14 25 4
gpt4 key购买 nike

我找到了这样的代码

public class Test {
static void myTest() {
TestInterface test = new TestInterface() {

@Override
public void test1() {
// TODO Auto-generated method stub
}

@Override
public boolean isOK() {
// TODO Auto-generated method stub
return false;
}
};
if (test.isOK()) test.test1();
}
}

interface TestInterface {
void test1();
boolean isOK();
}

如果它不仅仅是一个骨架,那么它编译得很好并且可能会起作用。我知道它创建了一个接口(interface)的隐式实现和该类的一个实例。尝试使用“java”“隐式”“接口(interface)”进行谷歌搜索并没有给我更多关于这个结构的背景信息。我缺少哪个搜索词?

最佳答案

这称为匿名类:Anonymous classes .

正如表达式所暗示的,它们没有名称,用于同时声明和实例化一个扩展另一个类(或实现接口(interface))的类。

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time.

这些类对于声明它们的方法是本地的。

关于java - 我在哪里可以找到有关此类隐式类实现的更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com