generic paradigm

await 2021-09-15 09:58:27

contrast extends and super wildcard So let's go back a little bit extends wildcard . As a method parameter , The difference between type and type is :

Allow the read method to be called T get() obtain T References to , But calling write methods is not allowed set(T) Pass in T References to ( Pass in null With the exception of );

Allow calls to write methods set(T) Pass in T References to , But the read method is not allowed to be called T get() obtain T References to ( obtain Object With the exception of ).

One is to allow reading but not writing , The other is to allow writing but not reading .

{collapse-item label=" Rubbing method "}

The wipe method determines the generic :

It can't be the basic type , for example :int; Cannot get with generic type Class, for example :Pair.class; Type with generic type cannot be judged , for example :x instanceof Pair; Can't instantiate T type , for example :new T(). Generic methods should prevent repeated definition of methods , for example :public boolean equals(T obj);

The subclass can get the generic type of the parent class .

{/collapse-item} {collapse-item label="extends wildcard "}

//extends wildcard Read allowed, write not allowed Using a wildcard like character as a method parameter indicates : Method can be called internally to get Number Quoted method , for example :Number n = obj.getFirst();; Method cannot be called internally Number Quoted method (null With the exception of ), for example :obj.setFirst(Number n);. That is to sum up in one sentence : Use extends Wildcards indicate that you can read , Can't write . When defining a generic class with a similar expression : The generic type is limited to Number as well as Number Subclasses of .

 Pair p1=new Pair<>(12.3,4.56);
int n=setsa(p2);// Read
System.out.println(n);
//extends wildcard Read allowed, write not allowed
static int setsa(Pair p){
Number fi=p.getFirst();
Number la=p.getLast();
return p.getFirst().intValue()+p.getFirst().intValue();
}
 class Pair {
private T first;
private T last;
public Pair(T first, T last) {
this.first = first;
this.last = last;}
public T getFirst() {return first;}
public T getLast() {return last;}
public void setFirst(T first) {this.first = first;}
public void setLast(T last) {this.last = last;}
}

{/collapse-item}

{collapse-item label="super wildcard "}

Use wildcards to indicate : Allowed to call set(? super Integer) Methods the incoming Integer References to ; Call is not allowed get() Methods to get Integer References to . The only exception is access to Object References to :Object o = p.getFirst(). let me put it another way , Use wildcards as method parameters , Indicates that the internal code of a method can only be written for parameters , Cannot read .

 //super wildcard
// be aware Pair Express ,
//Pair It's all Pair Superclass of
// Method parameters accept all generic types as Integer or Integer Of the parent class Pair type .
Pair p1=new Pair<>(12.3,4.56);
Pair p2=new Pair<>(123,456);
setSame(p1,456);
setSame(p2,123);// write in
Pairp3=p1;// Transition up safely
System.out.println(p3.getFirst()+","+p3.getLast());
System.out.println(p1.getFirst()+" "+p1.getLast());
System.out.println(p2.getFirst()+" "+p2.getLast());
//super wildcard Write not read allowed .
static void setSame(Pair p, Integer n) {
p.setFirst(n);
p.setLast(n);
}
class Pair {
private T first;
private T last;
public Pair(T first, T last) {
this.first = first;
this.last = last;}
public T getFirst() {return first;}
public T getLast() {return last;}
public void setFirst(T first) {this.first = first;}
public void setLast(T last) {this.last = last;}
}

{/collapse-item}

{collapse-item label="super Follow extends Application "}

copy() The definition of a method perfectly demonstrates extends and super The intent of the :

copy() Method will not read inside dest, Because you can't call dest.get() To get T References to ;

copy() Methods are not modified internally src, Because you can't call src.add(T).

// hold src Copy each element of to dest in :
public static void copy(List dest, List src) {
for (int i=0; i

{/collapse-item}

{collapse-item label=" other "}

{message type="info" content="

//Pair It's all Pair Superclass of
void sample(Pair p) {
}

Because wildcards have neither extends, either super, therefore :

Call is not allowed set(T) Method and pass in a reference (null With the exception of ); Call is not allowed T get() Method and get T quote ( Can only get Object quote ). let me put it another way , Can't read either , You can't write , That can only be done null Judge :"/}

{/collapse-item}

This article altogether 742 Number of words , Average reading time ≈ 2 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

Please bring the original link to reprint ,thank
Similar articles

2021-09-15