Home > Java > javaTutorial > How to use wildcards in java method parameters

How to use wildcards in java method parameters

PHPz
Release: 2023-05-01 10:37:06
forward
1096 people have browsed it

Explanation

1. Wildcard? super Fruit limits the lower bound of the generic to Fruit.

2. Generic objects and Generic objects can be passed to print2 as parameters. However, the Generic object cannot be passed in as a parameter, because Apple is a subclass of Fruit and exceeds the lower bound of generics.

Example

Generic<Fruit> fruitGeneric2 = new Generic<>();
print2(fruitGeneric2);
 
Generic<Food> foodGeneric2 = new Generic<>();
print2(foodGeneric2);
 
//    错误使用
//    Generic<Apple> appleGeneric2 = new Generic<>();
//    print2(appleGeneric2);
Copy after login

The above is the detailed content of How to use wildcards in java method parameters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template