I must admit that I am not a Java programmer. Daily development mainly involves C ++ and Delphi. I use Java to develop Android applications completely. Today, I am looking at Java's generics. Some aspects are very strange. Let's look at the following code:
Class shape {
Public void draw ()
{
System. Out. println ("Draw shape ");
}
}
Class rect extends shape
{
@ Override
Public void draw ()
{
System. Out. println ("Draw rect ");
}
}
Class line extends shape
{
@ Override
Public void draw ()
{
System. Out. println ("Draw line ");
}
}
Class drawer <t> {
Public
Void drawshape (T shape ){
Shape. Draw ();
}
}
Drawer is a generic class. The drawshape method is used to draw a graph. From the experience of using the C ++ template, this is absolutely correct, but Java has a compilation error: shape. draw calls are not allowed.
I modified the code again:
Class drawer <t> {
Public
Void drawshape (T shape ){
Shape. tostring ();
}
}
In this way, the compilation is passed. It seems that Java interprets T as an object.
Is there any way to interpret it as a shape? after reading the document, I know that I want to write it like this:
Class drawer <t extends shape>
{
Public void drawshape (T shape)
{
Shape. Draw ();
}
} Inheritance can also be specified for original type participation. If so, what is the difference between class drawer {
Public void
Drawshape (shape ){
Shape. Draw ();
}
}
It seems that Java's generics are quite different from C ++'s templates. Java's generics are mostly used for containers, and in my opinion, its biggest function is to save the operation of type conversion and check whether the type is correct during compilation. The traditional container class may need to be written as follows:
List intlist = new arraylist ();
Intlist. Add (New INTEGER (10 ));
Integer I =
(Integer) intlist. Get (0 );
With the generic type, you can write it like this:
List <integer> intlist = new
Arraylist <integer> ();
Intlist. Add (New INTEGER (10 ));
Integer I = intlist. Get (0 );
I also noticed that generic parameters cannot be basic types, but only objects. This is a further widening gap with the c ++ template. I feel that Java generics are not very useful, but complicated. For example, if wildcard characters are used, let's look at the following code:
Private Static void
Printlist (list <Object> List ){
For (Object O: List)
{
System. Out. println (O. tostring ());
}
}
Public static void dotest (){
List <rect> intlist = new arraylist <rect> ();
Intlist. Add (New rect ());
Intlist. Add (New rect ());
Intlist. Add (New rect ());
Printlist (intlist );
}
Printlist cannot be compiled because list <rect> is incompatible with list <Object>:
Private Static void printlist (list <?>
List ){
For (Object O: List)
{
System. Out. println (O. tostring ());
}
}
List <?>
It means that the element type of the List is unknown, but it is always normal to become an object, so it can be compiled. What should I do if I want it to be a shape, how to add extends with wildcards:
Class shape {
Public String
Getname (){
Return "shape ";
}
}
Class rect extends shape
{
@ Override
Public String getname ()
{
Return "rect ";
}
}
Public class testgenerics {
Private Static void printlist (list <?
Extends shape> List ){
For (shape S: List)
{
System. Out. println (S. getname ());
}
}
Public static void dotest ()
{
List <rect> intlist = new
Arraylist <rect> ();
Intlist. Add (New
Rect ());
Intlist. Add (New rect ());
Intlist. Add (New rect ());
Printlist (intlist );
}
}
Look at list <? Extends shape>
List, which means that the list item must be a shape or inherit from a shape. After a circle, return to the polymorphism to solve the problem.
However, this usage imposes some restrictions, that is, list <? The list in extends shape> list cannot add or delete elements, for example:
Private Static void printlist (list <?
Extends shape> List ){
For (shape S: List)
{
System. Out. println (S. getname ());
}
List. Add (New
Line ());
}
The list. Add (New Line () statement cannot be compiled because the collection class with wildcards cannot determine the type of Its element.
I personally think Java should not be generic, because there is no reason to use it. It can only help you automatically convert and check the type, but it brings about syntax complexity, in fact, it is not worth the candle.