可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
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
回答1:
Since the question specifically asks about kind of error
I will explain them with below scenarios
If you do below
Notebook[] labComputers = new Notebook[10];
Now you can only set Notebook objects in array.
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
You can read oracle docs about Polymorphism to know more how it works.
回答2:
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.
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
.
An example of those function implementation:
public Class Notebook extends Computer { void MoveMouse(); { MousePad.Move(); } } public Class Desktop extends Computer { void MoveMouse(); { USBMouse.Move(); } }
回答3:
Each subscripted variable labComputers[i]
can reference either a Computer
object or a Notebook
object.
This is technically true, but you must bear in mind that every Notebook
is a Computer
object, but not every Computer
is a Notebook
.
Therefore, if you had
Notebook[] labComputers = new Notebook[10];
you would not be able to place Computer
instances in the array because not every Computer
is a Notebook
- and your array can hold only Notebook
s.