Struggling wildcard in Java

I have a very basic question

The following code cannot be compiled (assuming Apple extensions fruit):

List<? extends Fruit> numbers = new ArrayList<>();
    numbers.add(new Apple());  //compile time error

When reading why not, I understand the words, but not the concept:)

Let's assume that the first fruit is not an abstract class I understand, because we are dealing with multiple subtypes, all of which extend fruit It is said that we can't add anything to the collection because we can't determine the exact type of fruit There are several things I don't understand:

1) Obviously, we can't know which fruit puzzles me When iterating over a collection, can't we tell a specific type through typeof or other instanceof checks?

2) Assuming that fruit is a concrete class, why are we not allowed to add instances of fruit? This seems to make sense because you at least know fruit's API Even if you don't know the exact subtype of fruit, at least you can call the standard method in fruit ()

I think this should be quite obvious, but some things are not for clicking on me Any help would be appreciated thank you!

Solution

The best way to understand this is to think of wildcards as statements about lists, not results To put it another way:

List<Banana> allBananas = getMyBananas();
enumerateMyFruit(allBananas);

static void enumerateMyFruit(List<? extends Fruit> myFruit) {
    for (Fruit fruit : myFruit)
        System.out.println(fruit);
}

When we pass allbanana to enumeratemyfruit, we lose information about the original declaration type of the list inside the method In this example, we can see very clearly why we should not, for example, put apple on the list because we know that the list is actually list < banana > Again, wildcards tell us something about the declaration type of the list

List & lt;? Extended fruit > should be understood as "a list originally declared to hold fruit or some subtype of fruit, but we don't know what the type of declaration is." All we know is that everything we extract from the list is fruit

In addition, you're right. We can iterate over the list and use instanceof to find out the real content in the list, but this won't tell us the original declaration type of the list In the above code snippet, we will find that everything in the list has become banana, but I can easily declare allbanana as list < fruit >

You may also see why a list < dog > is not a list < animal >, which explains some of them Wildcards are how we covariance between generic types List < dog > is not list < animal > but it is a list This makes it impossible for us to add to list & lt;? The restriction of extends animal > because it may be list < Dog & gt;, List < cat > or something We don't know

anything else? Which is the opposite We can store fruit in the list but we don't know what we're going to pull out of it The type of its original declaration may actually be, for example, a list < Object >, which contains various other contents

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>