我知道有两种方法可以创建匿名类来实例化 Scala 中的特征:
scala> trait SomeTrait {
| def aUsefulMethod = ()
| }
defined trait SomeTrait
scala> val instance1 = new SomeTrait{} // Method 1
instance1: SomeTrait = $anon$1@7307556f
scala> instance1.aUsefulMethod // Returns a Unit.
scala> object instance2 extends SomeTrait // Method 2
defined module instance2
scala> instance2.aUsefulMethod // Returns a Unit.
Run Code Online (Sandbox Code Playgroud)
我想不出它们不相等的原因。我错了吗?
我问这个问题的部分原因是我以前只知道方法 2,但现在我发现方法 1 更常见。所以我想知道我是否一直做错了什么。
我尝试实现类似于CollectionUtils 转换(Apache Commons Collections)的功能
class CollectionUtils {
public static void transformerModifier(Collection<MyClass> myCollection) {
// How should I implement this method in order that
// output from the line 1 and line 2 will be the same ?
}
public static List<String> transform(Collection<MyClass> myCollection) {
List<String> strCollection = new LinkedList<>();
for (MyClass item : myCollection) {
strCollection.add(item.getName());
}
return strCollection;
}
}
Run Code Online (Sandbox Code Playgroud)
class myClass {
private String name;
private int value;
myClass( String name, int value) {
this.name = name …Run Code Online (Sandbox Code Playgroud) 我知道匿名函数在 JS 中是如何工作的,并且对 Java 中的部分内容有点困惑。
所以在下面我有一个匿名类(我只是使用 Thread 类作为我所看到的示例),在那里我覆盖了run()函数,然后在该类上调用.start()。
new Thread() {
@Override
public void run() {
System.out.println("Hello from the anonymous class thread");
}
}.start();
Run Code Online (Sandbox Code Playgroud)
所以这是有效的,但 IntelliJ 想让我把它改写成这样:
new Thread(() -> System.out.println("Hello from the anonymous class thread")).start();
Run Code Online (Sandbox Code Playgroud)
我得到了大部分这种语法,但对如何覆盖run()函数有点困惑。根据我的理解,没有参数被传递到 Thread 类(所以没有任何参数被传递到我假设的构造函数中)。现在我感到困惑的地方就在这里。它没有在任何地方声明它覆盖了run()函数。这是Thread类的特例还是我遗漏了什么?
希望我清楚地解释了这一点,并提前致谢!
我正在尝试用Java创建简单的GUI程序,我找不到正确的错误解决方案,不能引用在不同方法中定义的内部类中的非final变量.
到目前为止,这是我的小代码;
myPanel = new JPanel();
JButton myButton = new JButton("create buttons");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int val = Integer.parseInt(textfield.getText());
for(int i = 0; i < val; i++) {
JButton button = new JButton("");
button.setText(String.valueOf(i));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickButton(i);
}
});
myPanel.add(button);
myPanel.revalidate();
}
}
});
Run Code Online (Sandbox Code Playgroud)
也许我的方法是完全错误的.我想做的是; 我想创建一组按钮并说当用户按下按钮时我想显示"你按下按钮4"或"你按下按钮10"这样的消息.
编译器如何知道嵌套类型,如:
.....等等
需要清晰.......
当我使用匿名类进行小型操作(如过滤集合)时,会在Java 8中为新的匿名类实例或闭包进行内存分配.
String firstNonEmpty = Lists.find(list, new Predicate<String>(){
public String apply(String s){ return !s.isEmpty();}
});
Run Code Online (Sandbox Code Playgroud)
我应该在Java 8中重用这样的谓词或闭包吗?始终/循环/无GC方法?
我之前问过这个问题,但我得不到合适的答案.
如果非最终字段的值可以更改,那么如何在匿名类类中使用它们?
class Foo{
private int i;
void bar(){
i = 10
Runnable runnable = new Runnable (){
public void run (){
System.out.println(i); //works fine
}//end method run
}//end Runnable
}//end method bar
}//end class Foo
Run Code Online (Sandbox Code Playgroud)
如果在匿名类中使用的局部变量必须final使编译器能够在匿名类代码中内联它们的值,如下所示:
之前:
public class Access1 {
public void f() {
final int i = 3;
Runnable runnable = new Runnable() {
public void run() {
System.out.println(i);
}//end method run
};//end anonymous class
}//end method f
}//end class Access1
Run Code Online (Sandbox Code Playgroud)
后:
public class Access1 …Run Code Online (Sandbox Code Playgroud) 为什么我不能在下面的代码中为匿名类的String成员分配新值?
尝试编译时抛出错误:未解决的编译问题:令牌";"上的语法错误 ,,期待.然而String s = "Value";,当线s = "New Value";被注释掉时,这就行了.
public class Example {
// nested class to help us out
private class SomeClass {
}
// entry point
public static void main (String args[]){
Example e = new Example();
e.performTest();
}
// performs the test
private void performTest(){
// anonymous class to test
SomeClass nc = new SomeClass() {
String s = "Value";
s = "New Value";
};
}
}
Run Code Online (Sandbox Code Playgroud) PHP 7引入了一个名为匿名类的新类功能,它允许我们创建对象而无需命名它们.匿名类可以嵌套.您对内存消耗,执行时间,性能问题有何看法?有没有可用的指标/统计数据?
anonymous-class ×10
java ×7
java-8 ×2
c# ×1
closures ×1
collections ×1
field ×1
final ×1
jvm ×1
lambda ×1
nested-class ×1
performance ×1
php ×1
php-7 ×1
scala ×1
swing ×1
traits ×1