Java generics are defined and for < Extends t> and <? Super t>

Source: Internet
Author: User

Definitions for generic methods in Java:

Public <T> T Gett () {

..... Related code;

}

One of my understanding of <T> is that it is just a token of a generic method, and T is the type of return.

For the definition of a generic class:

public class type<t>{

.... Related code

}

It is written about the definition of generic methods and generic classes. The main purpose of writing this blog here is to record < Extends t> and <? Super T> 's understanding.

<? Extends t> is the upper bound wildcard character. Logically, a container that can put animals in it can also be put in a dog, because dogs inherit animals, so from

Logically, a container that can put a dog should also inherit a container that can put animals. But this is not true in Java. The upper bound wildcard is required to solve this problem.

can define a content<? Extends animal> content, this content container can put any Animal including its subclasses. This allows the container content to be

A certain connection. But there are also some side effects. There is a getcontent () method inside the content class, which is not affected by the use of this method. Because for the Java compiler,

Everything in it has an identical father, and that is animal. But if you want to use the SetContent () method, then there is a problem, what is the problem? Then it

I only know that the type inside is animal and animal, but it is not possible to determine the specific type, dog or cat. The type here is not specified, so the compiler

It's just a placeholder, so when I put dog or apple in the back, he doesn't know if it matches the animal, so it's not allowed to use set. Below

The public void SetContent in the method (List<t> content) can be defined if you want to set T to include the List of subclasses of T:

public void SetContent (LIST<? Extends t> content).

<? Super T> is a nether wildcard. Its role this good and above the <? Extends t> opposite. content<? Super Animal> content represents Animal and Animal.

A container for a base class, but not a container for the dog base class. The SetContent () method inside can be used normally. Because it specifies the minimum granularity of the lower limit, that is animal. But it went out

GetContent () is rather laborious, because their public base class is only object. So the contents of the extract can only be loaded with object.

So here is summed up a point is if a content he needs to set the content of more, then use < Super t>. If you use get a bit more, then use < Extends T>.

Here is a content class of your own definition:

1  Packagetest;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 Importjava.util.List;6 7  Public classContent<t> {8     PrivateString ContentName;9     PrivateList<t>content;Ten     PrivateT T; One     Private intsize; A      PublicContent () { -Content =NewArraylist<t>(); -Size = 0; the     } -      PublicString Getcontentname () { -         returnContentName; -     } +      Public voidsetcontentname (String contentname) { -          This. ContentName =ContentName; +     } A      PublicList<t>getcontent () { at         returncontent; -     } -@SuppressWarnings ("Unchecked") -      Public voidSetContent (list<?extendsT>content) { -          This. Content = (list<t>) content; -     } in      Public voidAdd (T content) { -          This. Content.add (content); to     } +      Public voidGetInfo () { - System.out.println (content.tostring ()); the     } *      Public intGetSize () { $Size =content.size ();Panax Notoginseng         returnsize; -     } the      PublicT Gett () { +         returnT; A     } the      Public voidsett (t t) { +          This. T =T; -     } $      Public voidRemoveintPOS) { $          This. Content.remove (POS); -     } -      PublicT Get (inti) { the         if(i>content.size ()) -             return NULL;Wuyi         returnContent.get (i); the     }     -}

Define the Animal class:

1  Packagetest;2 3  Public classAnimal {4     PrivateString type;5     PrivateString describe;6      PublicAnimal () {7         8     }9      PublicAnimal (String type,string describe) {Ten          This. Type =type; One          This. describe =describe; A     } -      PublicString GetType () { -         returntype; the     } -      Public voidsetType (String type) { -          This. Type =type; -     } +      PublicString Getdescribe () { -         returndescribe; +     } A      Public voidSetdescribe (String describe) { at          This. describe =describe; -     } -      -}

Inherit animal's dog class

1  Packagetest;2 3  Public classDogextendsAnimal {4     PrivateString name;5     Private intAge ;6      PublicDog (String type,string describe,string name,intAge ) {7         Super(type,describe);8          This. Name =name;9          This. Age =Age ;Ten     } One      PublicString GetName () { A         returnname; -     } -      Public voidsetName (String name) { the          This. Name =name; -     } -      Public intGetage () { -         returnAge ; +     } -      Public voidSetage (intAge ) { +          This. Age =Age ; A     } at      -}

Java generics are defined and for < Extends t> and <? Super t>

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.