简体   繁体   English

Java-类型与实例类型

[英]Java - Type vs instance type

When I do something like: 当我做类似的事情时:

List<Integer> myList = new LinkedList<>();

Is the object "myList" of type List? 对象类型为List的“ myList”吗? I am more interested about the correct definition. 我对正确的定义更感兴趣。 Would it be correct to say that: 这样说是否正确:

myLyst is of type List but its instance is of type LinkedList? myLyst的类型为List,但其实例的类型为LinkedList?

That doesn't make sense to me because the object can be only one type. 这对我来说没有意义,因为对象只能是一种类型。 So maybe it would be better to say that it is of type LinkedList but restricted to the interface List? 因此,也许最好说它是LinkedList类型,但仅限于接口List?

myList has a concrete runtime class, and that is LinkedList . myList有一个具体的运行时类,即LinkedList

But LinkedList as a type, is a subtype of List , so it is also correct to say that myList is a List . 但是LinkedList作为类型,是List的子类型,因此说myList List也是正确的。

The thing that is definite at runtime is that an object has only one runtime class, which in this case is LinkedList . 在运行时确定的是,一个对象只有一个运行时类,在本例中为LinkedList This class can be read by calling myList.getClass() . 可以通过调用myList.getClass()来读取myList.getClass()

But checking type hierarchies, ie, whether an object is an instance of a given type (class or interface), doesn't require that the type it's being checked against is a class. 但是检查类型层次结构,即对象是否是给定类型的实例(类或接口),并不要求要检查其类型的是类。 So: 所以:

myList instanceof LinkedList //true
myList instanceof List //true
myList instanceof Collection //true
myList instanceof ArrayList //false, 
  //because it's not an instance of ArrayList, 
  //and LinkedList is not a subtype of ArrayList

The LinkedList class implements the List interface. LinkedList类实现List接口。 myList is an instance of the LinkedList class and is therefore of type LinkedList . myListLinkedList类的实例,因此是LinkedList类型。

Since an interface is not a type, myList is not of type List , as List , an interface, is not a type. 由于接口不是类型,因此myList的类型myListList ,因为List的接口不是类型。

I hope this answer should clear some things up? 我希望这个答案可以使事情变得清晰起来吗?

I would say that the static type of the variable is List and its runtime type is LinkedList. 我会说变量的静态类型是List,而其运行时类型是LinkedList。

This is obviously made possible by the fact that LinkedList implements List. LinkedList实现List的事实显然使得这成为可能。

The distinction is important when discussing overriding (inheritance and virtual calls) and overloading (different methods with different parameters — parameters only take the static type into account) 在讨论重写(继承和虚拟调用)和重载(具有不同参数的不同方法-参数仅考虑静态类型)时,区别非常重要。

Java List is an interface that extends Collection interface. Java List是扩展Collection接口的接口。 Pic shows the hierarchy 图片显示了层次结构

Now, correct way would be, to say that "myList" is an interface variable currently holding the instance of LinkedList class. 现在,正确的方法是,说“ myList”是当前保存LinkedList类实例的接口变量。 You can look it from another analogy as well, interface is just an abstract representation therefore we can never create an object of type List thus it can only be used to hold reference of object of the class that implements it. 您也可以从另一个类比中看它,接口只是一个抽象表示,因此我们永远无法创建类型为List的对象,因此它只能用于保存实现它的类的对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM