如果“迭代器"接口是如何创建“迭代器"的对象的? [英] If "Iterator" is an interface how is it possible to create object of "Iterator"?

查看:88
本文介绍了如果“迭代器"接口是如何创建“迭代器"的对象的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以只有几天我接触了集合类,偶然发现了一个叫做迭代器"的东西.经过几次撬动和戳戳之后,我再次迷失了docs.oracle.org,在那里我了解到Iterator实际上是一个接口,但我们仍然创建它的对象.

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();

是"itr"吗?不是对象??还是我错过了什么?成为接口对象不是不可能吗?

Is the "itr" not an object?? or am I missing something?? Wasn't it impossible to make object of an interface??

那是什么特别的东西

myPrecious.iterator(); ??

不是

new Iterator();实例化一个对象?

忘记提Java是我的第一门编程语言,所以请原谅我的愚蠢.

Edit : forgot to mention Javas is kinda my first programming language so forgive my stupidity.

推荐答案

是"itr"吗?不是对象?

Is the "itr" not an object??

这是参考.

不是不可能使对象成为接口吗?

Wasn't it impossible to make object of an interface??

您无法实例化接口.在这里,父类型(Iterator)引用引用了子类型的对象.

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(); ??

此处iterator是该类中的函数,其对象为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.

不是

new Iterator();实例化一个对象?

您可以使用关键字new实例化非抽象类.您可以在接口名称上使用new实例化匿名类,如此处所示作为示例.

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.

这篇关于如果“迭代器"接口是如何创建“迭代器"的对象的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆