英文:
What do we mean when we say instance?
问题
如果我们有一个继承自类A的类B,并且我们有以下代码:
A obj = new B();
那么obj变量是指向类A的实例还是类B的实例?
英文:
If we have class B that extends class A, and we have the following code:
A obj = new B();
Is obj variable refers to instance of A or B?
答案1
得分: 0
Sure, here's the translation:
obj变量是指向A类实例还是B类实例?
都是!
如果你拥有一只猫,你是拥有了一只猫还是一只动物?-- 你完全可以说你同时拥有了两者!
用代码来解释:
Animal a = new Cat();
a 是一个变量,它持有一个指向 Cat 对象的引用(同时也是一个 Animal 对象)。
所以...
...a 持有一个指向 Cat 的引用吗?是的。
...a 持有一个指向 Animal 的引用吗?是的。
英文:
> Is obj variable refers to instance of A or B?
Both!
If you own a cat, do you own a cat or an animal? -- You can rightfully say you own both!
Paraphrased in code:
Animal a = new Cat();
a is a variable that holds a reference to a Cat object (which at the same time is an Animal object).
So...
...does a hold a reference to a Cat? Yes.
...does a hold a reference to an Animal? Yes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论