JAVA course 42nd-generic (II)-generic interface & amp; wildcard Application
I. Generic Interfaces
Interface Inter
{Public void show (T t);} class InterImple implements Inter
{// It is known that the String type is public void show (String str) {System. out. println ("show" + str) ;}} class InterImple_2
Implements Inter{// Do not know what type it is. public void show (Q q) {System. out. println ("InterImple_2.show () +" + q) ;}} public class Main {public static void main (String [] args) {InterImple in = new InterImple (); in. show ("sf"); InterImple_2 In2 = new InterImple_2 (); In2.show (2 );}}
Ii. Generic limitations
Wildcard. Generally speaking, no matter what type, a symbol is used to represent T, E, X ,?...
Where are the wildcards? Not much different from the wildcard T-Area
Import java. util. arrayList; import java. util. collection; import java. util. hashSet; import java. util. iterator; public class Main {public static void main (String [] args) {ArrayList
Al = new ArrayList
(); Al. add ("ads"); al. add ("sfdf"); show (al); ArrayList
A3 = new ArrayList
(); A3.add (5); a3.add (6); show (a3); HashSet
Al 2 = new HashSet
(); Al2.add (1); al2.add (2); show (Al2O3) ;}// if it is simply printed, what is the wildcard? Public static void show (Collection
Al) {Iterator
It = al. iterator (); Iterator
It = al. iterator (); while (it. hasNext () {System. out. println (it. next () ;}/// if you need to return continue operation // public static
T show (Collection
Al) {// Iterator
Iterator = al. iterator (); // T t = iterator. next (); // return t ;//}}
About iterator
/* Iterator
It = al. iterator (); while (it. hasNext () {System. out. println (it. next ();} * // a more elegant for each loop is used after java5.0. iterator and for (T x: al) {System. out. println (x );}
Concise and clear
Demonstration of the first wildcard character:
Import java. util. ArrayList; import java. util. Collection; import java. util. Iterator; public class Main {public static void main (String [] args) {ArrayList
Al = new ArrayList
(); Al. add (new Worker ("abc", 12); al. add (new Worker ("dgh", 11); show (al); ArrayList
A3 = new ArrayList
(); A3.add (new Student ("ASD", 156); a3.add (new Student ("AFDFD", 16); show (a3 );} // if you only want to print Man's subclass // Collection
No, Collection
= New ArrayList
(); The left and right types do not match // so in the generic declaration, you can use the inherited public static void show (Collection
Al) {// Iterator
It = al. iterator (); while (it. hasNext () {Man man = it. next (); System. out. println (man) ;}/// equivalent to public static
Void show1 (Collection
Al) {// for (T xT: al) System. out. println (xT );}}
Therefore, there are two limits for generic storage:
Upper Limit :? Extends E ,? Only subclasses of the E type or E type can be accepted.
Lower limit :? Super E: accept the parent class of E type or E.
Iii. Upper Limit:
Import java. util. ArrayList; import java. util. Collection; import java. util. Iterator; public class Main {public static void main (String [] args) {ArrayList
Al1 = new ArrayList
(); Al1.add (new Man ("ABC", 12); al1.add (new Man ("DGH", 11); ArrayList
Al 2 = new ArrayList
(); Al2.add (new Worker ("abc", 12); al2.add (new Worker ("dgh", 11); ArrayList
A3 = new ArrayList
(); Al3.add (new Student ("abc", 12); al3.add (new Student ("dgh", 11); al1.addAll (Al ); // when an element is stored, it is the upper limit, because the removal is based on the upper limit type, and there is no type security risk System. out. println (al1.size ());}}
For more information, see the API documentation. addAll method: addAll (? Extends E), which can store all subclasses of E and E.
Iv. lower limit:
The lower limit can be used to retrieve elements from a set.
That is, no matter what type is stored (as long as it is a subclass of the current parent class), you can use the parent type to receive
Import java. util. Comparator; import java. util. TreeSet; import java. util. Iterator; class ComparaName implements Comparator
{// Sort by name public int compare (Man o1, Man o2) {// TODO Auto-generated method stubint t = o1.getName (). compareTo (o2.getName (); return t = 0? O1.getAge ()-o2.getAge (): t ;}} public class Main {public static void main (String [] args) {TreeSet
Al1 = new TreeSet
(New ComparaName (); al1.add (new Man ("ABC", 12); al1.add (new Man ("DGH", 11); TreeSet
Al 2 = new TreeSet
(); Al2.add (new Worker ("abc", 12); al2.add (new Worker ("dgh", 11); TreeSet
A3 = new TreeSet
(New ComparaName (); al3.add (new Student ("abc", 12); al3.add (new Student ("dgh", 11 )); // Add students and workers to the al1 set al1.addAll (_3); al1.addAll (Al); // sort Iterator according to Man's sorting rules.
It = al1.iterator (); while (it. hasNext () System. out. println (it. next ());}}
5. wildcard characters
Import java. util. Collection; import java. util. ArrayList; import java. util. Iterator; public class Main {public static void main (String [] args) {ArrayList
Al1 = new ArrayList
(); Al1.add (new Man ("ABC", 12); al1.add (new Man ("DGH", 11); ArrayList
Al 2 = new ArrayList
(); Al2.add (new Man ("ABC", 12); al2.add (new Man ("DGH", 11); boolean flag1 = al1.containsAll (Al); System. out. println (flag1); ArrayList
A3 = new ArrayList
(); Al3.add ("qwertyi"); al3.add ("asd"); boolean flag2 = al1.containsAll (_3); System. out. println (flag2);} // method: containsAll (Collection
C )//? The equals method in the Object is "asd ". equals (new Worker ("asd", 12), both compile and run through the public static void show (Collection
Al) {for (Iterator
It = al. iterator (); it. hasNext ();) {System. out. println (it. next ());}}}