在Java中编写一个类的实例。

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

Writing an instance of a class in java

问题

类名 a = new ClassName1();

ClassName 和 ClassName1 代表什么?
我原以为 ClassName 必须等于 ClassName1。

英文:

ClassName a = new ClassName1();

What does ClassName and ClassName1 mean?
I thought that ClassName had to equal ClassName1.

答案1

得分: 2

如果

public class ClassName1 extends/implements ClassName

那么这是完全有效的。

这就是多态的工作原理 - 你可以将给定的实例视为它是其任何超类或实现的接口的唯一实例。

英文:

If

public class ClassName1 extends/implements ClassName

than it is perfectly valid.

This is how polimorphism works - you can treat given instance as it would be a solely instance of any of its superclasses or interfaces it implements.

答案2

得分: 0

ClassName是变量a的类型,这意味着对于您的问题,a可以表示一个属于ClassName类型的对象(以及它的任何子类)。

因此,只有当ClassName1ClassName的子类时,句子ClassName a = new ClassName1()才有意义并且是正确的。

英文:

ClassName is the type of the variable a, which means for your problem, that a can represents an object of the type ClassName (and any other subclass of it).

So, the sentence ClassName a = new ClassName1() only make sense and is right if the ClassName1 is a subclass of ClassName.

huangapple
  • 本文由 发表于 2020年4月6日 03:48:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61048536.html
匿名

发表评论

匿名网友

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

确定