What Java generics are? Real demo

https://www.cnblogs.com/coprince/p/8603492.html

1 Overview

Generics have a very important role in java, there is a very wide range of applications in various object-oriented programming and design patterns.

What is a generic? Why use generics?

Copy the code
Generic, or "parameterized types." Mention of parameters, the most familiar with is visible when defining method parameters, and then call this method when passing arguments. So how to understand parameterized type it? 

As the name suggests, is a specific type as a parameter of the original, similar to the process variable parameter types are also defined at this time in the form of parameters (type parameter may be referred to),

and particularly when using the incoming / calling type (type arguments). Generic nature to parameterized type (not created in case of a new type, to control the type parameter particularly limited by different types of generic specified). That is the generic use,

the data type of the operation is specified as a parameter, which can be used in the type of classes, interfaces and methods, are called generic class, generic interfaces, generic method.
Copy the code

2. A chestnut

A lifted countless examples:

Copy the code
List arrayList = new ArrayList();
arrayList.add("aaaa");
arrayList.add(100);

for(int i = 0; i< arrayList.size();i++){
    String item = (String)arrayList.get(i);
    Log.d("泛型测试","item = " + item);
}
Copy the code

 

Guess you like

Origin www.cnblogs.com/naimao/p/12619395.html