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

How to use wildcards in java method parameters

PHPz
PHPzforward
2023-05-01 10:37:061090browse

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);

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete