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
//GenericGeneric2=new Generic<> (new Generic. Apple ());
GenericGeneric=new Generic<> (new Generic. Fruit ());
Generic. The setKey (new generic. Apple ());
}
}
CodePudding user response:
GenericGeneric2=new Generic<> (new Generic. Apple ());
You are good, this sentence is not an error
CodePudding user response:
an error