Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example analysis of Java generics upper bound and lower bound wildcard

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Java generics upper bound and lower bound wildcard example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Java generics upper bound and lower bound wildcard example analysis" bar!

Generics, inheritance, and subclasses

As you know, objects of one type can be assigned to objects of another type as long as they are compatible. For example, you can specify an integer and an object because the object is a supertype of an integer:

Object someObject = new Object (); Integer someInteger = new Integer (10); someObject = someInteger; / / OK

In object-oriented terminology, this is called a "is" relationship. Because Integer is an Object, assignment is allowed. But Integer is also a Number, so the following code is also valid:

Public void someMethod (Number n) {/ *... * /} someMethod (new Integer (10)); / / good someMethod (new Double (10.1)); / / OK

The same goes for generics. You can perform a generic type call, passing Number as its type parameter, and if the parameter is compatible with Number, allow any subsequent add calls:

Box box = new Box (); box.add (new Integer (10)); / / good box.add (new Double (10.1)); / / OK

Now consider the following methods:

Public void boxTest (Box n) {/ *... * /}

What kind of argument does it accept? By looking at its signature, you can see that it accepts a parameter of type Box. But what does it mean? Can you pass Box or Box as you expect? The answer is no, because Box and Box are not subtypes of Box.

This is a common misunderstanding when using generic programming, and it is also an important concept to learn.

Box is not a subtype of Box, even if Integer is a subtype of Number.

Note: given two specific types An and B (for example, Number and Integer), MyClass has nothing to do with MyClass, regardless of whether An and B are related or not. The common father of MyClass and MyClass is Object.

For information about how to create a similar subtype relationship between two generic classes when type parameters are related, see the wildcards and subtypes section below.

Generic classes and subtyping

You can subtype a generic class by extends it or by implementing a generic interface implements. The relationship between the type parameters of one class or interface and the type parameters of another class or interface is determined by the extends and implements clauses.

Using the Collections class as an example, ArrayList implements the List,List extension Collection. So ArrayList is a subtype of List, it is a subtype of Collection. Subtype relationships are preserved between types as long as the type parameters are not changed.

A chart showing an example of the Collection hierarchy: ArrayList is a subtype of List, and both are subtypes of Collection.

Now suppose we want to define our own list interface PayloadList, which associates the optional value generic type parameter P with each element. Its statement may be as follows:

Interface PayloadList extends List {void setPayload (int index, P val);...}

The following parameterization of PayloadList is a subtype of List:

PayloadListPayloadListPayloadList

Schematic diagram of the PayLoadList hierarchy: PayloadList is a subtype of List, which is a subtype of Collection. At the same level of PayloadList are PayloadList and PayloadList.

Wildcards and subtypes

As described in the section generics, inheritance, and subclasses, generic classes or interfaces are hardly related to their type parameters. However, you can use wildcards to create relationships between generic classes or interfaces.

Given the following two general (non-generic) classes:

Class A {/ *... * /} class B extends A {/ *... * /}

It makes sense to write the following code:

B b = new B (); An a = b

This example shows that the inheritance of a regular class follows this subtype rule: if B extends A, class B is a subtype of class A. This rule does not apply to generic types:

List lb = new ArrayList (); List la = lb; / / compile time error

Given that Integer is a subtype of Number, what is the relationship between List and List?

Although Integer is a subtype of Number, List is not a subtype of List. In fact, the two types are not related. The common parent of List and List is List.

Upper bound (extends) wildcards and lower bound (super) wildcards

To create relationships between these classes so that code can access Number methods through the elements of List, use the wildcards of the upper bound:

List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report