Generic class definition and generic method and generic limitation

Source: Internet
Author: User
1. The generic type defined by the generic class is valid throughout the class. If it is used by methods, all the types to be operated will be fixed after the object of the generic class is clear about the specific type to be operated.

2. In order to allow different methods to operate on different types, the types are still unknown. Then you can define generics on methods.

3. Special features:

Static methods cannot be generic defined on the generic class.

If the data type of the application operated by the static method is unknown, you can define the generic type on the method.

Package Tan; // defines generic classes (the types to be operated are not sure) Class demo <t> {public void show (t) {system. out. println ("show:" + T);} // define the generic public <q> void print (Q) {system. out. println ("pirnt:" + q);} // defines the generic public static <G> void method (g) {system. out. println ("mentodd:" + G) ;}} public class genericdemo1 {public static void main (string [] ARGs) {demo <string> d = new demo <string> (); D. show ("Beijing"); // D. show (6); Compilation failed, because the show method will only accept the type D with the object type and the object type. print (888); // define the generic type in the method upload. D. print ("Tan"); // class name calls static method demo. method ("It dreamer ");}}


4. Define generics on interfaces

Package Tan; // define the generic type on the Interface inter <t> {public void show (T );} // determine the operation type/* class interimpl implements inter <string >{@ overridepublic void show (string t) {system. out. println ("show -----" + T) ;}} * // Method 2: class interimpl <t> implements inter <t> {@ overridepublic void show (t) {system. out. println ("show -----" + T) ;}} public class genericdemo2 {public static void main (string [] ARGs) {/* interimpl impl = new interimpl (); impl. show ("great"); */interimpl <integer> I = new interimpl <integer> (); I. show (4 );}}

5 ,? Wildcard. It can also be understood as a placeholder.

Package Tan; import Java. util. *; public class genericdemo3 {public static void main (string [] ARGs) {arraylist <string> Al = new arraylist <string> (); Al. add ("ABC1"); Al. add ("abc2"); Al. add ("abc3"); arraylist <integer> al1 = new arraylist <integer> (); al1.add (4); al1.add (7); al1.add (1 ); printcoll (Al); printcoll (al1);} // 1. For ambiguous types, use placeholders to indicate public static void printcoll (arraylist <?> Al) {iterator <?> It = Al. iterator (); While (it. hasnext () {system. out. println (it. next (). tostring () ;}// 2. You can also use the following method: If T is a specific type, public static <t> void printcoll2 (arraylist <t> Al) {iterator <t> it = Al. iterator (); While (it. hasnext () {T = it. next (); system. out. println (t );}}}

6. Generic limitation

? Extends E: it can receive the E type or the sub-type of E. Upper limit.
? Super E: can receive the parent type of E or E. Lower limit


Upper Limit example:

Package Xiao; import Java. util. *; public class genericdemo3 {public static void main (string [] ARGs) {arraylist <person> Al = new arraylist <person> (); Al. add (new person ("tan11"); Al. add (new person ("tan13"); Al. add (new person ("tan21"); arraylist <student> al1 = new arraylist <student> (); al1.add (new student ("stu01 ")); al1.add (new student ("stu02"); al1.add (new student ("stu03"); printcoll (Al); system. out. printl N (); printcoll2 (Al); printcoll2 (al1);} // 1. Only the public static void printcoll (arraylist <person> Al) of the parent class can be printed) {iterator <person> it = Al. iterator (); While (it. hasnext () {system. out. println (it. next (). getname () ;}// 2. can I print the parent class or the upper limit of the subclass type? Extends E: can receive the public static void printcoll2 (arraylist <? Extends person> Al) {iterator <? Extends person> it = Al. iterator (); While (it. hasnext () {system. out. println (it. next (). getname () ;}} class person {private string name; person (string name) {This. name = Name ;}public string getname () {return name ;}} class student extends person {student (string name) {super (name );}} /* class student implements comparable <person> // <? Super E> {public int compareto (person s) {This. getname ()}*/

Lower limit example:

package zheng;import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;public class GenericDemo5 {public static void main(String[] args) {TreeSet<Student> ts = new TreeSet<Student>(new Comp());ts.add(new Student("abc1"));ts.add(new Student("abc2"));ts.add(new Student("abc3"));Iterator<Student> it = ts.iterator();while(it.hasNext()){System.out.println(it.next().getName());}}}class Person{private String name;Person(String name){this.name = name;}public String getName(){return name;}}class Student extends Person{Student(String name){super(name);}}class Comp implements Comparator<Person>//<? super E>{public int compare(Person s1,Person s2){//Person s1 = new Student("abc1");return s1.getName().compareTo(s2.getName());}}

General comparator method

Package Qiang; import Java. util. *; Class genericdemo4 {public static void main (string [] ARGs) {treeset <student> TS = new treeset <student> (New comp (); Ts. add (new student ("abc03"); Ts. add (new student ("abc02"); Ts. add (new student ("abc06"); Ts. add (new student ("abc01"); iterator <student> it = ts. iterator (); While (it. hasnext () {system. out. println (it. next (). getname ();} treeset <worker> ts1 = new treeset <worker> (New comp (); ts1.add (new worker ("WABC -- 03 ")); ts1.add (new worker ("WABC -- 02"); ts1.add (new worker ("WABC -- 06"); ts1.add (new worker ("WABC -- 01 ")); iterator <worker> it1 = ts1.iterator (); While (it1.hasnext () {system. out. println (it1.next (). getname () ;}}// the comparator is generic. Class comp implements comparator <person> {public int compare (person P1, person P2) {// only methods in the parent class can be used. If there is scalability, return p2.getname () is limited (). compareto (p1.getname () ;}} class person {private string name; person (string name) {This. name = Name;} Public String getname () {return name;} Public String tostring () {return "Person:" + name ;}} class student extends person {student (string name) {super (name) ;}} class worker extends person {worker (string name) {super (name );}}



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.