[英]Difference between Object[] and Array of specific Objects in java
I know this may be trivial but I could not find an answer yet.我知道这可能是微不足道的,但我还没有找到答案。
What are the differences between and Object[] and an array of specific Objects, for example和 Object[] 和特定对象的数组有什么区别,例如
class car {
public Object[] someCars;
public car[] someOthercars;
}
I know that car[] can store car Objects and Object[] can store any Objects.我知道 car[] 可以存储汽车对象,而 Object[] 可以存储任何对象。 But APART from this are there any not so obvious differences?
但除此之外,还有什么不那么明显的区别吗?
Car[]
can contain only Car
instances, or sub-classes of Car
instances. Car[]
只能包含Car
的情况下,或亚类Car
的实例。 If you were to have a class like this: class Honda extends Car
, Honda
instances can be populated in the Car
array as well, because Honda extends from Car
.如果你有一个这样的类:
class Honda extends Car
, Honda
实例也可以填充到Car
数组中,因为 Honda 从Car
扩展。
Object[]
can contain any Object
instances, or any subclass of Object
(every class in Java is a subclass Object
, even if it is not explicitly stated with an extends
keyword in a custom class), so any instance in java can be populated in the Object
array. Object[]
可以包含任何Object
的情况下,或任何子类Object
(Java中的每个类的子类Object
,即使它没有明确说明与extends
于自定义的关键字),所以在Java中的任何实例中可以填入Object
数组。
Answers to your questions are given here:您的问题的答案如下:
UPD UPD
Car
class inherits from Object
. Car
类继承自Object
。 An array of Car
s is narrower than an array of Object
s. Car
数组比Object
数组窄。 So roughly speaking if you store your cars in an array of objects - you will need to cast them back to cars each time you want to use an instance.因此,粗略地说,如果您将汽车存储在一组对象中 - 每次要使用实例时,您都需要将它们转换回汽车。 I doubt that you really need that.
我怀疑你真的需要那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.