英文:
If "Iterator" is an interface how is it possible to create object of "Iterator"?
问题
以下是翻译好的内容:
所以离我接触集合类只有几天时间,偶然间遇到了一个叫做“Iterator”的东西。经过一番探究,我又偶然间发现了docs.oracle.org网站,我从那里了解到Iterator实际上是一个接口,但我们仍然会创建它的对象。
Iterator itr = myPrecious.iterator();
这个“itr”难道不是一个对象吗?还是我漏掉了什么?难道不是不可能创建接口的对象吗?
那个特殊的东西是什么?
myPrecious.iterator();
不应该是
new Iterator();
来实例化一个对象吗?
编辑:忘记提到Java是我第一编程语言,所以请原谅我的愚笨。
英文:
So it's only been few days that I touched on collection classes and stumbled upon a thing called "Iterator". And after few prying and poking I again stumbled upon docs.oracle.org and there I learned that Iterator is actually an interface and still we create it's object.
Iterator itr = myPrecious.iterator();
Is the "itr" not an object?? or am I missing something?? Wasn't it impossible to make object of an interface??
and what is that special thing
myPrecious.iterator();
??
wasn't it
new Iterator();
to instantiate an object??
Edit : forgot to mention Javas is kinda my first programming language so forgive my stupidity.
答案1
得分: 2
> "itr"不是一个对象吗?
这是一个引用。
> 不可能创建接口的实例吗?
你不能实例化一个接口。这里,一个父类型(Iterator
)的引用正在引用一个子类型的对象。
> 那有什么特别的地方
>
> myPrecious.iterator();
?
在这里,iterator
是一个位于对象 myPrecious
所属类中的函数。查看该函数的定义,例如 这里。
> 那不是应该使用
>
> new Iterator();
来实例化一个对象吗?
你可以使用关键字 new
来实例化一个非抽象类。你可以通过在接口名称上使用 new
来实例化一个匿名类,就像这里的示例一样。
英文:
> Is the "itr" not an object??
It's a reference.
> Wasn't it impossible to make object of an interface??
You can not instantiate an interface. Here, a parent type (Iterator
) reference is referencing an object of child type.
> and what is that special thing
>
> myPrecious.iterator();
??
Here iterator
is a function in the class whose object is myPrecious
. Check the definition of the function, iterator
here for an example.
> wasn't it
>
> new Iterator();
to instantiate an object??
You can instantiate a non-abstract class using the keyword, new
. You can instantiate an anonymous class by using new
on the interface name as shown here for an example.
答案2
得分: 0
接口的目的是:
- 你无法直接创建接口的实例,你是对的,它是一个对象。
- 类可能会 实现 接口。它声明它实现了接口,并且它包含了接口包含的所有方法(除非它是一个抽象类,但你也不能从抽象类创建对象,所以暂时先忽略这种情况)。
- 你可以将一个实例(对象)的引用赋值给一个声明为接口类型的变量(或参数)。
所以来回答你的问题:
> Iterator itr = myPrecious.iterator();
>
> "itr" 不是一个对象吗?...
myPrecious.iterator()
返回一个真实的对象,并且对象的引用存储在 itr
中。这个对象可能属于我们没有听说过的某个类,我们也不需要关心。我们知道的是这个类实现了 Iterator
接口。这意味着我们可以按照接口规定使用这个迭代器。
> 不是吗
>
> new Iterator();
来实例化一个对象吗?
好问题。回答是:最终是这样。然而,在实际的编程中,我们经常调用普通方法以获取一个新对象。这个方法必须反过来使用 new
。或者调用另一个使用 new
的方法(依此类推)。这就是事情如何很好地协同进行的地方:因为我们不知道对象的实际类,所以我们不能自己使用 new
。但是 myPrecious
,你的集合对象,知道用于实例化迭代器对象的类,所以它可以为我们使用 new
。并且将创建的迭代器对象返回给我们。
通过这个小的代码实验,可以检查一下:
List<String> myList = new ArrayList<>();
Iterator itr = myList.iterator();
System.out.println(itr.getClass());
在我的 Java 11 上,它会输出:
> class java.util.ArrayList$Itr
在其他 Java 版本上的输出可能不同。你会注意到输出并没有将迭代器对象的类标记为 Iterator
接口,而是一些 ArrayList$Itr
。这意味着一个名为 Itr
的类在 ArrayList
类内部声明。所以是的,迭代器确实是属于一个类的对象。
英文:
The point of an interface is:
- You cannot directly create an instance of the interface, an object, you are correct.
- A class may implement the interface. It declares that it implements the interface and it contains all the methods that the interface contains (unless it’s an abstract class, but then again you can’t create objects from it, so let’s forget this situation for now).
- You may assign a reference to an instance (object) to a variable (or parameter) that is declared to have the interface type.
So to answer your questions:
> Iterator itr = myPrecious.iterator();
>
> Is the "itr" not an object?? …
myPrecious.iterator()
returns a real object, and a reference to the object is stored into itr
. The object probably belongs to some class that we haven’t heard of and do not need to care about. All that we know is that that class implements the Iterator
interface. This means that we can use the iterator as specified by that interface.
> wasn't it
>
> new Iterator();
to instantiate an object??
Good question. Answer: In the end it is. However, very often in real-world programming we are calling an ordinary method in order to get a new object. That method must in turn use new
. Or call another method that uses new
(etc.). And this is where things go nicely hand in hand: since we don’t know the actual class of the object, we cannot use new
ourselves. But myPrecious
, your collection object, does know the class to use for instantiating an iterator object, so it can use new
for us. And return the created iterator object to us.
One way to check is through this little code experiment:
List<String> myList = new ArrayList<>();
Iterator itr = myList.iterator();
System.out.println(itr.getClass());
On my Java 11 it prints:
> class java.util.ArrayList$Itr
Output on other java versions may be different. You notice that the output doesn’t mention the Iterator
interface as the class of the iterator object, but instead some ArrayList$Itr
. This means a class named Itr
declared inside the ArrayList
class. So yes, the iterator is really an object belonging to a class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论