To learn from Mr. Wang. Generics (ix): generics-Supplements

Source: Internet
Author: User

Generic Supplements
Keynote Teacher: Wang Shaohua QQ Group: 483773664 Learning Goals
    1. Mastering the difference between a generic method and a type wildcard

    2. Generics and Method overloading

    3. Generics and Arrays

The difference between a generic method and a type wildcard (a) Most of the time, you can use a generic method to replace the type wildcard character

For example, a requirement: Define a method, receive an arbitrary collection, and print out all the elements in the collection. Generic methods and type wildcards can all be implemented

12345678910111213 public class Need {    public <T> void printCollection(Collection<T> c){        for (T t : c) {            System.out.println(t);        }    }        public void printCollection2(Collection<?> c){        for (Object object : c) {            System.out.println(object);        }    }}
(b) Sometimes, we can use both generic methods and wildcard characters
12 public<T> void copy(List<T> dest,List<? extendsT> src){}

Of course, the above method can also be changed into a generic method

123 public<T ,E extends T> void copy2(List<T> dest,List<E> src){            }
(c) types can define variables
12345 public<T> void printCollection(Collection<T> c){    for(T t : c) {        System.out.println(t);    }}
(iv) Set<e> only allow elements of type E,set<?> equals set<? extends Object> only elements cannot be placed

Add (E)

Ii. generic method and method overloading

Method Overloading: Method overloading refers to defining multiple methods with the same name in a class, but requiring the number of types or parameters for each method to have different parameters.

(i) First case: the upper and lower limits of the generic type

Because generics allow you to set the upper limit of the wildcard character

So the following two methods are method overloads?

12345 public<T> void copy(List<T> dest,List<? extends T> src){}     public <T> void copy(List<? superT> dest,List<T> src){}

Reason

12345678 publicclass Test {    public static void main(String[] args) {        List<Number> c = new ArrayList<Number>();        List<Integer> c1 = new ArrayList<Integer>();        Need need = newNeed();        need.copy(c, c1);    }}

The compiler was unable to determine which method to call.

Of course, the above two methods at the time of compilation, it will not pass!

(ii) generic type specified differently
1234 publicvoid method(List<String> list) {}public voidmethod(List<Integer> list) {}

This notation is incorrect and the compilation fails.

This is because, from the Java language level, the method overloads depend on the same method name, the number of arguments, the type, the order, and the list<integer> and list<string> types are list<e> after erasure; This does not conform to the requirements of method overloading.

Note:

In Jdk6 and below, the following code compilation will pass, JDK7, compilation will not pass, JDK8 compilation will also pass

1234567 publicString test(List<String> list) {        return "";} public int test(List<Integer> list) {    return1;}

Iii. Generics and Arrays

(a), Java does not supportarray object of generic type1. You cannot create an array object that holds a generic type

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3D/wKiom1c78qHjYAiXAAAZCivwpPk262.png " data_ue_src= "E:\My knowledge\temp\15936359-39fb-4629-b73d-562060c83b24.png" >


2. Correct definition method

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3B/wKioL1c784uylavnAAAf2887VIU603.png " data_ue_src= "E:\My knowledge\temp\4dcc1ab8-0ba1-4a07-af89-bf167da0098f.png" >

3. Take value

<>

To learn from Mr. Wang. Generics (ix): generics-Supplements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.