Home > Back-end >  Would you please tell me why bosses generic class cannot be used in the method of constructing a sub
Would you please tell me why bosses generic class cannot be used in the method of constructing a sub

Time:01-16

As shown in the code
This is a personal definition of a generic class
 
Public class Generic {//T can be replaced by any letter
Public static class Fruit {

}
Public static class extends apple Fruit {

}
Private key T;

Public Generic (T T) {//Generic constructor parameter key types are T, T type by external specify
}

Public T getKey () {//generic method getKey the return value of type T, T type by external specify
The return key.
}
Public void setKey (T T) {
This. The key=t;
}
}

This is the concrete use, found in the creation process, transmit and cannot use the specified type subclass constructor, but when using methods and can be, not very understand why
 
Public class Question {
Public static void main (String [] args) {
//error
//Generic Generic2=new Generic<> (new Generic. Apple ());

Generic Generic=new Generic<> (new Generic. Fruit ());
Generic. The setKey (new generic. Apple ());
}

}

CodePudding user response:

 Generic Generic2=new Generic<> (new Generic. Apple ()); 


You are good, this sentence is not an error

CodePudding user response:

an error
  • Related