Java中的语法是什么意思:new Stream< Integer>(){...}? [英] What does the syntax mean in Java: new Stream<Integer>(){ ... }?
本文介绍了Java中的语法是什么意思:new Stream< Integer>(){...}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了下列无法识别的Java语法。
这部分很好:
public abstract class Stream< T>实现迭代器< T> {
public boolean hasNext(){
return true; }
public void remove(){
throw new RuntimeException(Unsupported Operation); }
}
但是我没有得到:
Stream< Integer> ones = new Stream< Integer>(){
public Integer next(){
return 1; }
};
while(true){
System.out.print(ones.next()+,);它是什么?
$ b 这是提供 Stream
类的内联(匿名)子类$ b 在功能上,它是相同的:
public NewClass extends Stream {
public Integer next ){
return 1;
}
}
和
void someMethodInAnotherClass {
Stream stream = new NewClass();
}
,但由于此类定义不在方法体外使用,将其定义为匿名。
I have encountered the following Java syntax that I don't recognize.
This part is fine:
public abstract class Stream<T> implements Iterator<T> {
public boolean hasNext() {
return true; }
public void remove() {
throw new RuntimeException("Unsupported Operation"); }
}
But this I don't get:
Stream<Integer> ones = new Stream<Integer>() {
public Integer next() {
return 1; }
};
while(true){
System.out.print(ones.next() + ", ");
}
What it is?
解决方案 This is providing an inline (anonymous) subclass of the Stream
class.
Functionally, it is the same as:
public NewClass extends Stream {
public Integer next() {
return 1;
}
}
and
void someMethodInAnotherClass {
Stream stream = new NewClass();
}
but as this class definition isn't used outside the method body, you can define it as anonymous.
这篇关于Java中的语法是什么意思:new Stream< Integer>(){...}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文