Java generics comprehensive parsing of __ generics

Source: Internet
Author: User
Tags class definition

Generics in Java can be said to allow novice confused, and the use of multiple generics at the same time can make a person face. Let's take a look at each situation now.
1. Simplest generic examples

public class Ttest<t> {public

    void Test () {
        list<t> List = new arraylist<> ();
    }

    public static void Main (String args[]) {
        ttest<string> t = new ttest<string> ();
        T.test ();
    }

This is the simplest generic, you can see in the TTest class definition of the place to write a <t>, this is the definition of generics, the class inside the method (except static method), you can use this class, specifically what meaning.

You can see,ttest<string> T in the main () method, where the <String> is passed in, the list<t> t = new arraylist<> () in the test () method. actually becomes list<string> t = new arraylist<> (); The main meaning of generics is to qualify or give a given type. Of course the generics of this <T> name can be arbitrarily taken such as <U>,<X> and so on. 2. Use of multiple generics

public class Ttest<t, u> {public

    void Test () {
        list<t> tlist = new arraylist<> ();
        list<u> ulist = new arraylist<> ();
    }

    public static void Main (String args[]) {
        ttest<string, integer> t = new ttest<> ();
        T.test ();
    }

There are two generic <t, U&gt, which are written to ttest<string,integer> T and then in the class for easy and enjoyable use, and two generics are more common, such as hashmap<k, v>.
3. Inheritance of generics
public class Ttest<t extends ttest.base> {public

    static class base{

    } public

    static class child extends base{

    } public

    void Test () {
        list<t> tlist = new arraylist<> ();
    }

    public static void Main (String args[]) {
        ttest<string> st = new Ttest<> ();//Compile Error
        ttest<base> BT = new ttest<> ()//compile correct
        ttest<child> ct = new ttest<> ();//compile correct
    }

}

Here you can see <t extends ttest.base> that is to say, the generics I passed in are inherited from the Base class, so you can see in the new TTest that if you write a ttest<string>, you compile the error. Because a string is not inherited from base. Written ttest<base> or ttest<child> are correct, because the child is inherited from Base.
4. The use of generics in static methods
public class Ttest<t> {public

    static <M> list<m> get () {
        class<m> m;//compile correct
        class <T> t;//Compile error return
        null;
    }

    public void Test () {
        ttest<integer>.<string>get ()//Compile error because calling a static method does not require a generic ttest.<string of the Write class
        >get ()//compile correct
    }
}

Because the static method is actually belong to the class, does not belong to the object, so the generic definition of generics, in the static method can not be used, will compile errors, such as the above code in the Get () method inside the class<t> will compile the error.

In the case of generics to be used in static methods, you need to redefine, add generic <m> between the static keyword and the return type, and then in the return type or the method body, you can use the generic. When used, the Ttest.<string>get () is invoked, and the public static <M> List<m>get () method becomes public static list<string >get (), the method body inside of the class<m> also become class<string>, in fact, is to replace <M> into <String>.


Section:

I've talked about several generic usage scenarios before, the advantage of using generics is that you know the type of incoming, you can go directly to the type you want in the definition of the method, without requiring the caller of the method to make a strong turn, the Findviewbyid () method in Android is a good example. The new version uses generics and does not require the caller to strongly turn the control's type.




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.