[Generic] _ generic entry notes
Objectives of this chapter:
Understanding the significance of generics:
Master the basic usage of generics:
Understanding the warning information and extensive erasure of generics
3.1 why use generics?
Now we need to design a class that can represent coordinate points. Coordinates are composed of X and Y. There are three methods to represent coordinates:
Integer, decimal, and string representation
Class Point {private object X; // indicates X coordinate private object y; // indicates y coordinate public void setx (Object X) {This. X = x;} public void sety (Object y) {This. y = y;} public object getx () {return this. x;} public object Gety () {return this. Y ;}}; public class genericsdemo01 {public static void main (string ARGs []) {point P = new point (); // declare a point object p. setx (10); // use the automatic packing operation: int --> integer --> Object p. sety (20); // use the automatic packing operation: int --> integer --> Object int x = (integer) p. getx (); // The retrieved data is converted to integer first, and then int y = (integer) P is automatically split. gety (); // The retrieved data is first converted to an integer, and then the system is automatically split. out. println ("integer representation, X coordinate:" + x); system. out. println ("integer representation, Y coordinate:" + Y );}};
Class Point {private object X; // indicates X coordinate private object y; // indicates y coordinate public void setx (Object X) {This. X = x;} public void sety (Object y) {This. y = y;} public object getx () {return this. x;} public object Gety () {return this. Y ;}}; public class genericsdemo02 {public static void main (string ARGs []) {point P = new point (); // declare a point object p. setx (10.5f); // use the automatic packing operation: float --> Object p. sety (ipv6f); // use the automatic packing operation: float --> Object float x = (float) p. getx (); // The data to be retrieved is first converted to an integer, and then the data is automatically split into float y = (float) p. gety (); // The retrieved data is first converted to an integer, and then the system is automatically split. out. println ("decimal representation, X coordinate:" + x); system. out. println ("decimal representation, Y coordinate:" + Y );}};
Class Point {private object X; // indicates X coordinate private object y; // indicates y coordinate public void setx (Object X) {This. X = x;} public void sety (Object y) {This. y = y;} public object getx () {return this. x;} public object Gety () {return this. Y ;}}; public class genericsdemo03 {public static void main (string ARGs []) {point P = new point (); // declare a point object p. setx ("Dong Jing 180 degrees"); // string --> Object p. sety ("210 degrees north latitude"); // string --> Object string x = (string) p. getx (); // The retrieved data is converted to an integer first, and then the string y = (string) P is automatically split. gety (); // The retrieved data is first converted to an integer, and then the system is automatically split. out. println ("string representation, X coordinate:" + x); system. out. println ("string representation, Y coordinate:" + Y );}};
3.2 understanding generics
Generic Type can solve the security problem of data types. Its main principle is to use an identifier to indicate the type of an attribute in the class or the return value and parameter type of a method during class declaration. In this way, you only need to specify the required type when declaring or instantiating a class.
Definition Format of generic classes:
[Access permission] class name <generic type 1, generic type 2,..., generic type 3> {
[Access permission] Name of a variable of the generic type;
[Access permission] generic type identity method name (){}
[Access all lines] Return Value Type Declaration method name (generic type identification variable name )()
}
Generic object definition:
Class Name <specific class> Object Name = new class name <specific class> ();
Class point <t> {// you can write the identifier here. t is short for private t var. // The type of VaR is specified by T, that is: public t getvar () {// The type of the returned value is determined by the external return var;} public void setvar (T var) {// The type set is also determined by the external this. var = var ;}}; public class genericsdemo06 {public static void main (string ARGs []) {point <string> P = new point <string> (); // The VaR type is string type P. setvar ("mldn"); // sets the string system. out. println (P. getvar (). length (); // obtain the length of the string }};
Class point <t> {// you can write the identifier here. t is short for private t var. // The type of VaR is specified by T, that is: public point (T var) specified externally {// set the content through the constructor this. var = var;} 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 genericsdemo08 {public static void main (string ARGs []) {point <string> P = new point <string> ("mldn "); // The VaR type is string type system. out. println ("content:" + P. getvar ());}};
Class point <t> {// you can write the identifier here. t is short for private t var. // The type of VaR is specified by T, that is: public t getvar () {// The type of the returned value is determined by the external return var;} public void setvar (T var) {// The type set is also determined by the external this. var = var ;}}; public class genericsdemo07 {public static void main (string ARGs []) {point <integer> P = new point <integer> (); // The VaR type is string type P. setvar ("mldn"); // set the string }};
The above code will generate an error because the type value cannot match!
3.3 Use generics in Constructor
Format:
[Access permission] Constructor ([<generic type> parameter name]) {}
Class point <t> {// you can write the identifier here. t is short for private t var. // The type of VaR is specified by T, that is: public point (T var) specified externally {// set the content through the constructor this. var = var;} 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 genericsdemo08 {public static void main (string ARGs []) {point <string> P = new point <string> ("mldn "); // The VaR type is string type system. out. println ("content:" + P. getvar ());}};
3.4 set multiple generics
Class notepad <K, V >{// two generic type private K keys are specified here; // 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 <string, integer> T = NULL; // define two generic objects T = new notepad <string, integer> (); // the key in the object is string and the value is Integer T. setkey ("Li Xinghua"); // set the first content T. setvalue (30); // set the second content system. out. print ("name;" + T. getkey (); // obtain the information system. out. print (", age;" + T. getvalue (); // get information }};
3.5 generic security warning
Class info <t> {private t var; Public t getvar () {return this. vaR;} public void setvar (T var) {This. var = var;} Public String tostring () {// overwrite the tostring () method return this in the object class. var. tostring () ;}}; public class genericsdemo10 {public static void main (string ARGs []) {info I = new info (); // warning, no generic type is specified. setvar ("mldn"); // sets the string system. out. println ("content:" + I. getvar ());}};
Note: genericsdemo10.java uses unchecked or insecure operations.
Note: For details, use-xlint: unchecked to recompile.
The actual effect of the above Code is as follows:
Class info <t> {private t var; Public t getvar () {return this. vaR;} public void setvar (T var) {This. var = var;} Public String tostring () {// overwrite the tostring () method return this in the object class. var. tostring () ;}}; public class genericsdemo11 {public static void main (string ARGs []) {info <Object> I = new info <Object> (); // specify the object as the generic type I. setvar ("mldn"); // sets the string system. out. println ("content:" + I. getvar ());}};