Today, let's think about var and generics, multiple interface implementation.
Implement multiple interfaces
In the implementation of JDK and our daily work, many interfaces need to be implemented. Let's take two common examples, ArrayList and CopyOnWriteArrayList. First look at their definition:
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class CopyOnWriteArrayList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
We can see that ArrayList and CopyOnWriteArrayList implement the four interfaces of List, RandomAccess, Cloneable and Serializable.
More at www.flydean.com
If we have an ArrayList creator, which can create one of ArrayList or CopyOnWriteArrayList, how should we write the code?
public Object createList(boolean flag){ if(flag){ return new ArrayList<>(); }else{ return new CopyOnWriteArrayList<>(); } }
Because the returned value may be ArrayList or CopyOnWriteArrayList, we can only replace the Object to be returned with Object.
If an Object is returned, there is no advantage brought by generics. Is there any way to let us know exactly what type of Object is to be returned?
The first thing you think about is to create a new interface, inherit List, RandomAccess, Cloneable, java.io.Serializable, and then createList will return the newly created interface.
public interface ListInterface<E> extends List<E>, RandomAccess, Cloneable, java.io.Serializable { }
Then rewrite the createList method:
public <E> ListInterface<E> createListWithInterface(boolean flag){ if(flag){ return (ListInterface<E>) new ArrayList<E>(); }else{ return (ListInterface<E>) new CopyOnWriteArrayList<E>(); } }
The new method can be generic, and clearly indicates that a ListInterface is to be returned.
There is no problem in using the newly generated ListInterface in your own code. Consider that if your code is referenced by others or used as a public library by others, the third party may not know what your newly created ListInterface is.
Can we use a more intuitive way to create a List? The answer, of course, is yes. Here's an example:
public <T extends List<E> & RandomAccess & Cloneable & java.io.Serializable, E> T createListWithInterfaceT(boolean flag){ if(flag){ return (T) new ArrayList<E>(); }else{ return (T) new CopyOnWriteArrayList<E>(); } }
In the above example, we use generic t to inherit four interfaces at the same time. Then convert the created List to T and return.
In this way, we get the common types of ArrayList and CopyOnWriteArrayList, and we do not need to create a new interface.
Using multiple interfaces
Above we created a generic T that implements multiple interfaces. So what if you want to use it?
public <T extends List<E> & RandomAccess & Cloneable & java.io.Serializable, E> void useGenericityType(){ VarGenericity varGenericity=new VarGenericity(); T list=varGenericity.createListWithInterfaceT(true); }
In order to use T within a method, we must restate the definition of T above the method definition.
This can realize our functions, but it's too much trouble.
Using var
At this time, var variable can be used instead. Let's see the following example:
public void useVarInGenericityType(){ VarGenericity varGenericity=new VarGenericity(); var list=varGenericity.createListWithInterfaceT(true); }
Is it simple, and the var list variable retains all the public methods of the four interfaces. Finally note: the theory of light is not enough. By the way, I'll send you ten sets of practical tutorials and interview question bank of the latest JAVA architecture project in 2020, which can be found under the transformation of seven bar umbrella bar and Zero clothing umbrella (Digital homophony), and also can communicate with the old architect
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling