这段代码中的两个类分别指代什么?

huangapple go评论67阅读模式
英文:

What do the two classes in this code refer to?

问题

Item i1 = new CD();
我不明白如何从一个类实例化一个对象并将其放入另一个类中。
请解释一下ItemCD这两个类分别指的是什么?

英文:

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.

huangapple
  • 本文由 发表于 2020年3月15日 21:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定