Java generic explanation (General generic, wildcard, generic interface, Generic Array, generic method, generic nesting)
JDK 5.0 has long been expected, but it was changed to version when it was released. This shows that Java has changed significantly. This article describes the new features supported by JDK-Java generics.
1. Java generic
In fact, Java generic is to create a class with the type as the parameter. Just like the method of writing a class, the method is such a method (String str1, String str2). The values of str1 and str2 in the method are variable. The same is true for generics. In this way, writing class Java_Generics <K, V> is like the str1 and str2 parameters in the method.
Note the following when writing generic classes:
1) when defining a generic class, define the formal type parameters between "<>", for example, "class TestGen <K, V>", where "K ", "V" does not represent a value, but a type.
2) when instantiating a generic object, you must specify the value (type) of the type parameter after the class name, which must be written twice in total. For example:
TestGen <String, String> t = new TestGen <String, String> ();
3) in the generic <K extends Object>, extends does not represent inheritance. It is a type range restriction.
General generic
Java code
- Class Point {// You can write the identifier here. T is short for type.
- Private T var; // The type of var is specified by T, that is, by external
- Public T getVar () {// The type of the returned value is determined by the external
- Return var;
- }
- Public void setVar (T var) {// The set type is also determined by the external
- This. var = var;
- }
- };
- Public class GenericsDemo06 {
- Public static void main (String args []) {
- Point P = new Point (); // The var type is String type.
- P. setVar (it); // set the string
- System. out. println (p. getVar (). length (); // obtain the string length.
- }
- };
- ----------------------------------------------------------
- Class Notepad {// Two generic types are specified here
- Private K key; // The type of this variable is determined by the external
- Private V value; // The type of this variable is determined by the external
- Public K getKey (){
- Return this. key;
- }
- Public V getValue (){
- Return this. value;
- }
- Public void setKey (K key ){
- This. key = key;
- }
- Public void setValue (V value ){
- This. value = value;
- }
- };
- Public class GenericsDemo09 {
- Public static void main (String args []) {
- Notepad T = null; // defines two generic objects.
- T = new Notepad (); // The key in it is String, and the value is Integer.
- T. setKey (Tom); // set the first content
- T. setValue (20); // sets the second content.
- System. out. print (name; + t. getKey (); // obtain information
- System. out. print (, age; + t. getValue (); // obtain information
-
- }
- };
Wildcard
Java code
- Class Info {
- Private T var; // defines generic Variables
- Public void setVar (T var ){
- This. var = var;
- }
- Public T getVar (){
- Return this. var;
- }
- Public String toString () {// print directly
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo14 {
- Public static void main (String args []) {
- Info I = new Info (); // Use String as the generic type
- I. setVar (it); // set content
- Fun (I );
- }
- Public static void fun (Info Temp) {// can receive any generic object
- System. out. println (content: + temp );
- }
- };
Restricted Generic
Java code
- Class Info {
- Private T var; // defines generic Variables
- Public void setVar (T var ){
- This. var = var;
- }
- Public T getVar (){
- Return this. var;
- }
- Public String toString () {// print directly
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo17 {
- Public static void main (String args []) {
- Info I1 = new Info (); // Declare the Integer generic object
- Info I2 = new Info (); // Declare the Float generic object
- I1.setVar (30); // you can specify an integer for automatic packing.
- I2.setVar (30.1f); // you can specify decimals for automatic packing.
- Fun (i1 );
- Fun (i2 );
- }
- Public static void fun (Info Temp) {// only the subclass of the Number and its Number can be received.
- System. out. print (temp + ,);
- }
- };
- ----------------------------------------------------------
- Class Info {
- Private T var; // defines generic Variables
- Public void setVar (T var ){
- This. var = var;
- }
- Public T getVar (){
- Return this. var;
- }
- Public String toString () {// print directly
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo21 {
- Public static void main (String args []) {
- Info I1 = new Info (); // Declare a String generic object
- InfoI2 = new Info(); // Declare the generic Object of the Object
- I1.setVar (hello );
- I2.setVar (new Object ());
- Fun (i1 );
- Fun (i2 );
- }
- Public static void fun (Info Temp) {// you can only receive String or Object-type generics.
- System. out. print (temp + ,);
- }
- };
The generic model cannot be transformed upwards.
Java code
- Class Info {
- Private T var; // defines generic Variables
- Public void setVar (T var ){
- This. var = var;
- }
- Public T getVar (){
- Return this. var;
- }
- Public String toString () {// print directly
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo23 {
- Public static void main (String args []) {
- Info I1 = new Info (); // The generic type is String.
- InfoI2 = null;
- I2 = i1; // This sentence will cause an error incompatible types
- }
- };
Generic Interface
Java code
- Interface Info {// Define the generic type on the Interface
- Public T getVar (); // defines the abstract method. The return value of the abstract method is of the generic type.
- }
- Class InfoImpl Implements Info {// Defines the subclass of the generic interface
- Private T var; // define attributes
- Public InfoImpl (T var) {// set the attribute content through the constructor
- This. setVar (var );
- }
- Public void setVar (T var ){
- This. var = var;
- }
- Public T getVar (){
- Return this. var;
- }
- };
- Public class GenericsDemo24 {
- Public static void main (String arsg []) {
- Info I = null; // declare an interface object
- I = new InfoImpl (Tom); // instantiate an object through subclass
- System. out. println (content: + I. getVar ());
- }
- };
- ----------------------------------------------------------
- Interface Info {// Define the generic type on the Interface
- Public T getVar (); // defines the abstract method. The return value of the abstract method is of the generic type.
- }
- Class InfoImpl implements Info {// Defines the subclass of the generic interface
- Private String var; // define attributes
- Public InfoImpl (String var) {// sets the attribute content through the constructor
- This. setVar (var );
- }
- Public void setVar (String var ){
- This. var = var;
- }
- Public String getVar (){
- Return this. var;
- }
- };
- Public class GenericsDemo25 {
- Public static void main (String arsg []) {
- Info I = null; // declare an interface object
- I = new InfoImpl (Tom); // instantiate an object by subclass
- System. out. println (content: + I. getVar ());
- }
- };
Generic Method
Java code
- Class Demo {
- Public T fun (T t) {// can receive any type of data
- Return t; // directly return the Parameter
- }
- };
- Public class GenericsDemo26 {
- Public static void main (String args []) {
- Demo d = new Demo (); // instantiate the Demo object
- String str = d. fun (Tom); // pass the String
- Int I = d. fun (30); // pass the number, automatically boxed
- System. out. println (str); // output content
- System. out. println (I); // output content
- }
- };
Returns a generic instance using the generic method.
Java code
- Class Info {// Specify the upper limit, which can only be numbers
- Private T var; // This type is determined by the external
- Public T getVar (){
- Return this. var;
- }
- Public void setVar (T var ){
- This. var = var;
- }
- Public String toString () {// override the toString () method in the Object class
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo27 {
- Public static void main (String args []) {
- Info I = fun (30 );
- System. out. println (I. getVar ());
- }
- Public static Info Fun (T param) {// The generic type passed in or returned by the method is determined by the parameter type set when the method is called.
- Info Temp = new Info (); // Instantiate Info based on the input data type
- Temp. setVar (param); // set the passed content to the var attribute of the Info Object.
- Return temp; // return the instantiated object
- }
- };
Unified input parameter types using generics
Java code
- Class Info {// Specify the upper limit, which can only be numbers
- Private T var; // This type is determined by the external
- Public T getVar (){
- Return this. var;
- }
- Public void setVar (T var ){
- This. var = var;
- }
- Public String toString () {// override the toString () method in the Object class
- Return this. var. toString ();
- }
- };
- Public class GenericsDemo28 {
- Public static void main (String args []) {
- Info I1 = new Info ();
- Info I2 = new Info ();
- I1.setVar (HELLO); // set content
- I2.setVar (Tom); // set content
- Add (i1, i2 );
- }
- Public static Void add (Info I1, Info I2 ){
- System. out. println (i1.getVar () ++ i2.getVar ());
- }
- };
Generic Array
Java code
- Public class GenericsDemo30 {
- Public static void main (String args []) {
- Integer I [] = fun1 (,); // returns a Generic Array
- Fun2 (I );
- }
- Public static T [] fun1 (T... arg) {// receives variable parameters
- Return arg; // returns a Generic Array.
- }
- Public static Void fun2 (T param []) {// output
- System. out. print (receiving Generic Array :);
- For (T t: param ){
- System. out. print (t + ,);
- }
- }
- };
Nested settings of generics
Java code
- Class Info {// Receive two generic types
- Private T var;
- Private V value;
- Public Info (T var, V value ){
- This. setVar (var );
- This. setValue (value );
- }
- Public void setVar (T var ){
- This. var = var;
- }
- Public void setValue (V value ){
- This. value = value;
- }
- Public T getVar (){
- Return this. var;
- }
- Public V getValue (){
- Return this. value;
- }
- };
- Class Demo{
- Private S info;
- Public Demo (S info ){
- This. setInfo (info );
- }
- Public void setInfo (S info ){
- This.info = info;
- }
- Public S getInfo (){
- Return this.info;
- }
- };
- Public class GenericsDemo31 {
- Public static void main (String args []) {
- Demo > D = null; // use Info as the wildcard type of the Demo.
- Info I = null; // Info specifies two generic types
- I = new Info (Tom, 30); // instantiate the Info Object
- D = new Demo > (I); // set the Info class object in the Demo class
- System. out. println (Content 1: + d. getInfo (). getVar ());
- System. out. println (content 2: + d. getInfo (). getValue ());
- }
- };