"Java-based build a shelf," I start from the basics of Java - the use of generics

A. The use of generics

1. Overview and use of generic classes

  • A: Overview generic classes: the generic class defined in the
  • B: Definition Format: public class name of the class <generic type 1, ...>
  • C: Note: The generic type must be a reference type

2. Overview and use of generic methods

  • A: Generic Method Overview: The generic method defined in
  • B: Definition Format: public <generic type> return type names (variable names generic type)
public <T> void show(T t) {

}

The so-called generic method is to define one or more parameters in a type declaration method. Usage format generic method is as follows:

修饰符<T, S> 返回值类型 方法名(形参列表){
   方法体
}

Points to note :

  • Parameter declaration method defined in the method can only be used where the type parameter interface defined in the class declaration may be used throughout the interface class. When calling fun () method, according to the actual object passed, the compiler determines that the actual type represented by the type parameter T.
class Demo{  
  public <T> T fun(T t){   // 可以接收任意类型的数据  
   return t ;     // 直接把参数返回  
  }  
};  
public class GenericsDemo26{  
  public static void main(String args[]){  
    Demo d = new Demo() ; // 实例化Demo对象  
    String str = d.fun("汤姆") ; // 传递字符串  
    int i = d.fun(30) ;  // 传递数字,自动装箱  
    System.out.println(str) ; // 输出内容  
    System.out.println(i) ;  // 输出内容  
  }  
};

3. Overview and use of generic interface

First look at a case

  • A: Overview Generic Interface: The interface is defined in the generic
  • B: Definition Format: public interface interface name <generic type>
/**
 * 泛型接口的定义格式:        修饰符  interface 接口名<数据类型> {}
 */
public interface Inter<T> {
    public abstract void show(T t) ;
}

/**
 * 子类是泛型类
 */
public class InterImpl<E> implements Inter<E> {
    @Override
    public void show(E t) {
        System.out.println(t);
    }
}

Inter<String> inter = new InterImpl<String>() ;
inter.show("hello") ;

Then look at the source code using the generic, is below after JDK 1.5, List interfaces, and code segments ArrayList class.

//定义接口时指定了一个类型形参,该形参名为E
public interface List<E> extends Collection<E> {
   //在该接口里,E可以作为类型使用
   public E get(int index) {}
   public void add(E e) {} 
}

//定义类时指定了一个类型形参,该形参名为E
public class ArrayList<E> extends AbstractList<E> implements List<E> {
   //在该类里,E可以作为类型使用
   public void set(E e) {
   .......................
   }
}

This is the essence of generic: allows the definition of the interface, class type parameter declaration, the type parameter in the entire interface, the class type used in vivo as almost everywhere to use conventional type can use this type of parameter.

Generic class subclass

  • After creating an interface with the generic declaration, the parent class may be created for the interface implementation class, from the parent class or subclass, Note: These interfaces can not contain the parent class type parameter subclassed , you need to pass specific type.
  • Wrong way:
public class A extends Container<K, V>{}
  • Right way:
public class A extends Container<Integer, String>{}
  • You may not specify a particular type, then the system will K, V as a parameter of type Object processing. as follows:
public class A extends Container{}

4. Summary and use of generic classes

Define a container class, storage key for key-value, of the uncertainty of the key type, can be defined using generic, designated as K and V.

public class Container<K, V> {

    private K key;
    private V value;

    public Container(K k, V v) {
        key = k;
        value = v;
    }

    public K getkey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public void setKey() {
        this.key = key;
    }

    public void setValue() {
        this.value = value;
    }

}

When using the Container class, you need only specify K, particularly type V can, creating different logical instances Container, used to store different types of data.

public static void main(String[] args) {
    Container<String,String>  c1=new Container<String ,String>("name","hello");
    Container<String,Integer> c2=new Container<String,Integer>("age",22);
    Container<Double,Double>  c3=new Container<Double,Double>(1.1,1.3);
    System.out.println(c1.getKey() + " : " + c1.getValue());      
    System.out.println(c2.getKey() + " : " + c2.getValue());                                                               
    System.out.println(c3.getKey() + " : " + c3.getValue());
}

In JDK 1.7 adds a generic "diamond" syntax: Java allow generic information after the completion of construction does not require a belt, as long as the given angle brackets (<>) can, Java can be inferred angle brackets should be What generic information. As follows:

Container<String,String> c1=new Container<>("name","hello");
Container<String,Integer> c2=new Container<>("age",22);

Overview The generic constructor

  • As the method allows generic type parameters declared in the method signature as, Java also allows the declaration of a type parameter in the constructor signature, thus creating a so-called generic constructor.
  • And no distinction as common generic method used a generic parameter is explicitly specified, the other is implicitly inferred if it is explicitly specified explicitly specified places subject to the type of the parameter, if the parameter passed specify the type and type of arguments do not match, it will compile error.
    public class Person {
    public <T> Person(T t) {
        System.out.println(t);
    }
    }
  • how to use
    public static void main(String[] args){
    //隐式
    new Person(22);
    //显示
    new<String> Person("hello");
    }

The only special note here is this:

If the constructor is generic constructor, while the case of the class is a generic class of how to use the generic constructors: because generic constructor can explicitly specify your own type parameter (need to use diamond, in the constructor before device), and their generic class also need to specify the type arguments (on the diamond after the constructor), which also appeared in two of the diamond, which there will be some small problems, the specific use to sum up here.

The representatives of the following examples

public class Person<E> {
    public <T> Person(T t) {
        System.out.println(t);
    }
}

This usage: Person <String> a = new <Integer> Person <> (15); this syntax is not allowed, it will directly compile error!

II. High of generics wildcards

1. Why use wildcards

Wildcards design some scenarios, such as in the use of generics, first declare a class Animal, and then declare a class Cat Animal class inheritance, apparently Cat Animal class is a subclass of the class, but not the List of List subtype, but often we need to express this in the program logic. To address this similar scene, on the basis of the generic type parameters of the new wildcard usage.

2. <? Extends T> upper bound wildcard

Upper bound wildcard name implies, <? Extends T> represents the upper bound of the type comprising [itself], so Wild parameterized type may be T or a subclass.

  • Because not determine what specific type, add approach is limited (you can add null, because null means any type), but can be assigned after obtaining element from the list to the parent type. As a first example of the figure above, the third add () action is limited, because a List List and List <? Extends Animal> subtype.
它表示集合中的所有元素都是Animal类型或者其子类
List<? extends Animal>

This is called the upper limit of a wildcard, and the keyword extends achieved, upon instantiation of the specified type only argument is a rear type subclass or extends itself.

  • E.g:
  • This will determine the type of elements in the collection, although the specific type of uncertainty, but at least know its parent. And other operations.
//Cat是其子类
List<? extends Animal> list = new ArrayList<Cat>();

3. <? Super T> lower bound wildcard

Lower bound wildcard <? Super T> represents the parameterized type is a supertype of T (including itself), the layers of first, until the Object

  • The compiler can not determine get () Returns what type of object is thus get () method is limited. May be made add () method, add () method and the type T can add subtype of type T, such as the second example of a first add Cat type object, the object is then added two types of Cat subclasses, which method is feasible, but if you add an object of type Animal, obviously the inheritance relationship backwards, is not feasible.
它表示集合中的所有元素都是Cat类型或者其父类
List <? super Cat>

This is called the lower limit of wildcards, to achieve super keyword, when instantiated, specify the type argument extends only type or subclass itself.

E.g:

//Shape是其父类
List<? super Cat> list = new ArrayList<Animal>();

4. <?> *** wildcard

  • Any type, if not explicitly, then that Object as well as any of the Java class
  • *** wildcard use <?> Said? Represents any kind of type that can represent any type of only null (Object itself can be considered a type, but not on behalf of any type, so the List and List the meaning is different, the former type is Object, which is the top of the inheritance tree, while the latter type is completely unknown).

III. Use only generic reference type

When declaring class instance of a generic type parameter reference type must be passed, you can not use basic types

  • For example, for User generic class, the following statement is illegal
    User<int,double> user=new User<>(1,10,0);

How to solve

  • You can use wrapper types to solve the problem

Guess you like

Origin blog.51cto.com/14637764/2460657