英文:
What do the two classes in this code refer to?
问题
Item i1 = new CD();
我不明白如何从一个类实例化一个对象并将其放入另一个类中。
请解释一下Item和CD这两个类分别指的是什么?
英文:
Item i1 = new CD();
I don't understand how I can instantiate an object from a class and put it in another class?
Please explain what do the classes Item and CD refer to?
答案1
得分: 2
据推测,CD 要么扩展了 Item,或者如果 Item 是一个接口,那么 CD 就实现了它。
英文:
Presumably, CD either extends Item, or, if Item is an interface, CD implements it.
答案2
得分: 0
Item i1定义变量,并允许其保存对类型为Item的对象的引用。new CD();创建了一个类型为CD的对象,并返回对它的引用,该引用被赋值给变量i1。我们可以在类CD是类/接口Item的子类型时执行此操作。
英文:
Item i1 defines variable, and allows it to hold reference to objects of type Item. new CD(); creates object of type CD and returns reference to it which is assigned to variable i1. We can do it when class CD is subtype of class/interface Item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论