JAVA basics-general summary
Concept:
Generics are new features of Java SE 1.5. The nature of generics is parameterized, that is, the Data Type operated is specified as a parameter. This type of parameter can be used to create classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods. The advantage of introducing generics in Java is that it is secure and simple.
Common characters of the generic type mean:
? Indicates an uncertain java type.
T indicates the java type.
K v represents the Key Value in the java Key Value.
E Represents the Element.
The following is a good example on cnblog.
It's really good. I wrote it for a long time. When I saw it, I immediately deleted it ~
General generic
- 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
- 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"); // sets the content
- Fun (I );
- }
- Public static void fun (Info Temp) {// can receive any generic object
- System. out. println ("content:" + temp );
- }
- };
Restricted Generic
- 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.
- 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
- 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
- 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.
- 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
- 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"); // sets the content
- I2.setVar ("Tom"); // set the content
- Add (i1, i2 );
- }
- Public static Void add (Info I1, Info I2 ){
- System. out. println (i1.getVar () + "" + i2.getVar ());
- }
- };
Generic Array
- 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
- 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 ());
- }
- };
Generic ErasureDue to the type erasure problem, all generic type variables are replaced with the original type. This leads to a problem. Since all are replaced with the original type, why do we not need to perform forced type conversion when obtaining it? Let's take a look at the ArrayList and get methods:
public E get(int index) { RangeCheck(index); return (E) elementData[index];}
We can see that the conversion is strong according to the generic variables before return. Assume that the generic type variable is Date. Although the generic information is erased, the (E) elementData [index] is compiled into (Date) elementData [index]. Therefore, we do not need to perform strong conversion on our own.
Generic And Difference And It contains a new concept of JAVA5.0. Because of their appearance, many people misunderstand their purposes:
1.
First, you can easily misunderstand it as a set of all classes inherited from T. This is a big mistake.
I believe you have seen or used List. Right? Why is it wrong to understand it as a set? If it is understood as a set, why not use List? ? So It is not a set, but the meaning of a subclass of T. Remember that it is a single and single type. The problem arises because of the uncertainty of the connected type, therefore, it is impossible to add elements by adding. Why do you still think "add (T)" is not working? Because Is a subclass of T. containers that can be placed in the subclass may not be placed in the superclass, that is, they may not be placed in T.
2.
It is easier to use here, no So many restrictions here mean that a class with T class as the lower limit is simply a super class of T class. But why can I add (T? Because a container that can be put into a certain type can certainly be placed into its subclass, the concept of polymorphism.