java接口和实例_java – 接口和实例化
第二個聲明是錯誤的:
Then it goes on and says a paragraph or so later that “You can declare a variable to be of an interface type for eg. Iterator loop = new Iterator();"
您確實可以聲明變量Iterator循環;您可以使用返回Iterator的方法,實現Iterator的類的構造函數或使用匿名內部類來初始化它,但是您無法直接實例化Iterator.
編輯:
我在網上找到了這本書.但第五個問題.以下是報價:
Remember that almost everywhere that you can use a class, you can use an interface instead. For example, you can declare a variable to be of an interface type:
Iterator loop = new Iterator() When a variable is declared to be of an interface type, it simply means that the object is expected to have implemented that interface. In this case, because Iterator contains an object of the type Iterator, the assumption is that you can call all three of the inter- face’s methods on that object: hasNext(), next(), and remove().
太棒了!有第6版的書包含如此愚蠢的錯誤!難以置信的…
總結
以上是生活随笔為你收集整理的java接口和实例_java – 接口和实例化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webrtc 语音流java_通过Web
- 下一篇: mysql索引碎片产生原因_解析mysq