Java generics detailed: and class use. Generic class, detailed usages of generic methods __java

Source: Internet
Author: User
Tags gety
First, introduce

1, the generic is what first tell everyone arraylist is generic type. That ArrayList can accomplish what unexpected function. Let's take a look at the following code:

[Java]View plain copy arraylist<string> strlist = new arraylist<string> ();   arraylist<integer> intlist = new arraylist<integer> ();   arraylist<double> doublelist = new arraylist<double> (); Everyone is familiar with the ArrayList, which constructs three lists, each with string, Integer, and double; This is the extraordinary thing about ArrayList: that all types of variables can be assembled into a corresponding list, Instead of implementing a build ArrayList class for each type. Here may not understand, the opening is always difficult, below see if there is no generics, how do we do;
2, no generics will doLet's look at the following code:
We implement two classes that can set the point coordinates, and set the point coordinates of the integer type and the point coordinates of the float type, respectively:
[Java]  View plain  copy//Set integer type point coordinates    class integerpoint{        private Integer x ;       //  representing x coordinates        private Integer y ;       //  Represents y-coordinate        public void setx (integer x) {            this.x = x ;       }        public void sety (integer y) {            this.y = y ;       }        public integer getx () {            return this.x ;       }       public  Integer&nbsP;gety () {           return this.y ;       }  }  //Set point coordinates of float type    class floatpoint{        private float x ;       //   Show X coordinates        private Float y ;        //  represents y-coordinate        public void setx (float x) {            this.x = x ;        }       public void sety (float y) {            this.y = y ;       }        public float getx () {            return this.x ;       }       public float  gety () {           return this.y ;       }  }   Now there is a question: Have you found that they except variable type, one is the integer one is float, the other is no different. Can we merge into one, then?
The answer is yes, because integer and float are derived from object, we use the following code instead:
[Java]View Plain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.