我们说”instance”时是什么意思?

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

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.

huangapple
  • 本文由 发表于 2020年9月14日 04:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63875273.html
匿名

发表评论

匿名网友

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

确定