java 子类型多态性和数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12878879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtype polymorphism and arrays
提问by user133466
Computer[] labComputers = new Computer[10];
with
和
public class Computer {
...
void toString(){
// print computer specs
}
}
public class Notebook extends Computer{
...
void toString(){
// print computer specs + laptop color
}
}
each subscripted variable labComputers[i]
can reference either a Computer
object or a Notebook
object because Notebook
is a subclass of Computer
. For the method call labComputers[i].toString()
, polymorphism ensures that the correct toString
method is called.
每个下标变量labComputers[i]
都可以引用一个Computer
对象或一个Notebook
对象,因为它Notebook
是Computer
. 对于方法调用labComputers[i].toString()
,多态确保调用正确的toString
方法。
I wonder what if we do
我想知道如果我们这样做会怎样
Notebook[] labComputers = new Notebook[10];
what kind or error would I get if I reference with Computer
object and a Notebook
object
什么样的或错误的,我会得到,如果我有引用Computer
对象和Notebook
对象
回答by Amit Deshpande
Since the question specifically asks about kind of error
I will explain them with below scenarios
由于问题具体询问kind of error
我将用以下场景解释它们
If you do below
如果你在下面做
Notebook[] labComputers = new Notebook[10];
Now you can only set Notebook objects in array.
现在您只能在数组中设置 Notebook 对象。
labComputers[0] = new Notebook(); // Fine
labComputers[1] = new Computer(); // Compilation error
Now if you do
现在如果你这样做
Computer[] notebooks = new Notebook[10];
notebooks[0] = new Notebook();
notebooks[1] = new Computer(); // <--- ArrayStoreException
Because arrays are covarant
,reified
in nature ie. if Sub
is a subtype of Super
, then the array type Sub[]
is a subtype of Super[]
, and arrays enforce their element types at run time it will cause ArrayStoreException
因为数组是covarant
,reified
在本质上即。如果Sub
是 的子类型Super
,则数组类型Sub[]
是 的子类型Super[]
,并且数组在运行时强制其元素类型会导致 ArrayStoreException
You can read oracle docs about Polymorphismto know more how it works.
您可以阅读有关多态的oracle 文档以了解其工作原理。
回答by ForceMagic
I think you have to understand how polymorphism works.
我认为您必须了解多态性是如何工作的。
Polymorphism is a feature that allow multiples data type to behave the same way through a common interface.
多态性是一种特性,它允许多个数据类型通过公共接口以相同的方式运行。
For instance,
例如,
Computer // Base class
| |
Notebook Desktop // Both inherits of Computer
Polymorphism would allow you to manage an array of Computer, no matter if they are a Notebook or a Desktop.
多态允许您管理一组计算机,无论它们是笔记本还是台式机。
Computer[] computerArray = new Computer[2];
computerArray[0] = new Notebook();
computerArray[1] = new Desktop();
The advantage of this, is that you don't have to know which subtype of computer you are working with. They will behave as computer.
这样做的好处是,您不必知道您使用的是哪种计算机子类型。他们将表现得像计算机。
Now comes the big difference, in your Computer class you could have :
现在有很大的不同,在你的计算机课上你可以有:
public Class Computer
{
abstract void MoveMouse();
}
This will give you the opportunity to redefine this method differently in Notebook and Desktop. MoveMouse() will now be available to computeArray because we defined it in Computer.
这将使您有机会在 Notebook 和 Desktop 中以不同的方式重新定义此方法。MoveMouse() 现在可用于计算数组,因为我们在 Computer 中定义了它。
If you do this:
如果你这样做:
computerArray[0].MoveMouse(); // which contains a Notebook
computerArray[1].MoveMouse(); // which contains a Desktop
that will call the function that is implemented in Notebook
or Desktop
.
这将调用在Notebook
or 中实现的函数Desktop
。
An example of those function implementation:
这些函数实现的一个例子:
public Class Notebook extends Computer
{
void MoveMouse();
{
MousePad.Move();
}
}
public Class Desktop extends Computer
{
void MoveMouse();
{
USBMouse.Move();
}
}
回答by arshajii
Each subscripted variable
labComputers[i]
can reference either aComputer
object or aNotebook
object.
每个下标变量
labComputers[i]
都可以引用一个Computer
对象或一个Notebook
对象。
This is technically true, but you must bear in mind that every Notebook
is a Computer
object, but notevery Computer
is a Notebook
.
这在技术上是正确的,但您必须记住,每个Notebook
都是一个Computer
对象,但并非每个Computer
都是Notebook
.
Therefore, if you had
因此,如果你有
Notebook[] labComputers = new Notebook[10];
you would notbe able to place Computer
instances in the array because not every Computer
is a Notebook
- and your array can hold only Notebook
s.
您将无法Computer
在数组中放置实例,因为并非每个Computer
都是 a Notebook
- 并且您的数组只能容纳Notebook
s。