In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of array covariance and paradigm invariance in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I. covariation, invariance and inversion
Let's say I wrote this code for a restaurant.
Class Soup {public void add (T t) {}} class Vegetable {} class Carrot extends Vegetable {}
There is a paradigm class Soup, which represents the soup made of food T, and its method add (T t) means to add food T to the soup. Class Vegetable represents vegetables and class Carrot represents carrots. Of course, Carrot is a subclass of Vegetable.
So the question is, what is the relationship between Soup and Soup?
First of all, Soup should be a subclass of Soup, because carrot soup is obviously a vegetable soup. If that's true, take a look at the following code. Where Tomato stands for tomato, which is another subclass of Vegetable
Soup soup = new Soup (); soup.add (new Tomato ())
The first sentence is OK, Soup is a subclass of Soup, so you can assign an instance of Soup to the variable soup. The second sentence is fine, because soup is declared to be of type Soup, its add method takes a parameter of type Vegetable, and Tomato is Vegetable, which is of the correct type.
However, there is a problem with putting the two sentences together. The actual type of soup is Soup, and we pass an instance of Tomato to its add method! In other words, we are making carrot soup out of tomatoes, and we certainly can't make it. Therefore, although it is logically smooth to regard Soup as a subclass of Soup, it is flawed in the process of use.
So what exactly is the relationship between Soup and Soup? Different languages have different understandings and implementations. To sum up, there are three situations.
(1) if Soup is a subclass of Soup, then generic Soup is said to be covariant.
(2) if Soup and Soup are two unrelated classes, then the generic Soup is said to be invariant.
(3) if Soup is the parent of Soup, then generic Soup is said to be inverted. (however, inverters are not common)
Understand the concepts of covariance, invariance and inversion, and then look at the implementation of Java. The general generics of Java are immutable, that is, Soup and Soup are two unrelated classes, and you cannot assign an instance of one class to a variable of another. Therefore, the above code that uses tomatoes to make carrot soup cannot be compiled at all.
2. Array covariance
In Java, arrays are basic types, not generics, and there is no such thing as Array. But it is similar to generics in that it is a type built with another type. Therefore, arrays should also be considered for denaturation.
Unlike the invariance of generics, Java's array is covariant. That is, Carrot [] is a subclass of Vegetable []. The example in the previous section has shown that covariation can sometimes cause problems. For example, the following code
Vegetable [] vegetables = new Carrot [10]; vegetables [0] = new Tomato (); / / runtime error
Because the array is covariant, the compiler allows you to assign Carrot [10] to variables of type Vegetable [], so this code can be compiled smoothly. It was only when JVM really tried to insert a tomato into a pile of carrots that he found something bad at runtime. So, the above code throws an exception of type java.lang.ArrayStoreException at run time.
Array covariance is one of the famous historical baggage of Java. Be careful when using arrays!
If you replace the array in the example with List, the situation will be different. Like this
ArrayList vegetables = new ArrayList (); / / compile time error vegetables.add (new Tomato ())
ArrayList is a generic class that is immutable. Therefore, there is no inheritance relationship between ArrayList and ArrayList, and this code will report an error at compile time.
Although both pieces of code report errors, in general, compile-time errors are easier to handle than run-time errors.
Third, when generics also want to covariant and invert
Generics are immutable, but we still want it to change in some scenarios. For example, there is a little sister who drinks vegetable soup to lose weight every day.
Class Girl {public void drink (Soup soup) {}}
We hope that the drink method can accept a variety of vegetable soups, including Soup and Soup. However, due to the limitation of immutability, they cannot be used as parameters of drink.
To achieve this, a method similar to covariance should be used.
Public void drink (Soup
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.