Java genenic Fan Form

Source: Internet
Author: User

One reason to use the paradigm:

For example: Now you want to set up a point-and-click class with X-coordinates and y-coordinates. The two properties of this point require X, y to hold an int, float, and string type. Then you can do the following design, the type of x, Y is set to object to realize the arbitrary parameter

classPoint {PrivateObject x; PrivateObject y;  PublicObject GetX () {return  This. x; }         PublicObject GetY () {return  This. Y; }         Public voidSetX (Object x) { This. x =x; }         Public voidsety (Object y) { This. y =y; }}

1. representing coordinates in integers

 Public classTest { Public Static voidMain (string[] args) {point P1=NewPoint (); P1.setx (3);//automatic boxing operation int integer ObjectP1.sety (5);//automatic boxing operation int integer Object//remove x, y coordinates         intx = (Integer) p1.getx ();//Automatic unpacking operation Interger int        inty = (Integer) p1.gety ();//Automatic unpacking operation Interger intSystem.out.println (x);    System.out.println (y); }        }/*Error: 1. Integer i is not capitalized, note that the first letter of each word in the class name is uppercase. */

Output: 3

5

2. Using strings to represent coordinates

 Public class Test {    publicstaticvoid  main (string[] args) {        new Point ();        P1.setx ("Longitude 3");        P1.sety ("Latitude 5");                 // remove x,    y coordinates         string x = (String) p1.getx ();         = (String) p1.gety ();                SYSTEM.OUT.PRINTLN (x);        System.out.println (y);    }        }

Output: Longitude 3

Latitude 5

3. If there is a transition error like the following

 Public class Test {    publicstaticvoid  main (string[] args) {        new Point ();        P1.setx ("3");        P1.sety ("Latitude 5");                 // remove x,    y coordinates         int x = (Integer) p1.getx ();         int y = (Integer) p1.gety ();                SYSTEM.OUT.PRINTLN (x);        System.out.println (y);    }        }

The syntax of the program is not a problem and can be compiled correctly. An error occurs while the program is running

Output: java.lang.ClassCastException:java.lang.String cannot is cast to Java.lang.Integer

Because the properties in the point class are received using the object class, there is a security issue, a generic reference to solve the problem.

Use of two generic types

The principle of definition: primarily when a class declaration identifies an attribute type in a class or the return type of a method by an identity, the form parameter type

1. Generic parameters
classPerson<i,n>{I age;    N name;        String Skincolor; Person (I age,n name,string skincolor) { This. Age =Age ;  This. Name =name;  This. Skincolor =Skincolor; }             PublicString toString () {return  This. age.tostring () + "" + This. name.tostring () + "" + This. skincolor.tostring (); }} Public classTest { Public Static voidMain (string[] args) { person<Integer,String> P1 =NewPerson<integer,string> (25, "Lei Feng", "Black");        System.out.println (P1.tostring ()); }}/*Error: 1. Person<int,string> It's supposed to be typed as an integer.*/

Output: 25 Lei Feng Black

The person class above is a generic class because it is used in the constructor method of person.

2. Generic interface

Application One: Specify the specific type directly in the interface

Comparable interface and CompareTo are defined as follows

Interface comparable<t>
int CompareTo (T o)

classPersonImplementsComparable<person> {    intAge ;    String name; Person (intage,string name) {         This. Age =Age ;  This. Name =name; }             PublicString toString () {return  This. Age + "" + This. Name; }         Public intcompareTo (person p) {if( This. Age >p.age) {return1; }    Else if( This. Age <p.age) {return-1; }Else {            return  This. Name.compareto (P.name); }    }} Public classTest { Public Static voidMain (string[] args) {person P1=NewPerson (21, "Zhang San"); Person P2=NewPerson (22, "John Doe");        System.out.println (P1.compareto (p2)); }}/*error: 1. this.age.toString () + "" + this.name; Error: cannot dereference int*/

Output: –1

Java genenic Fan Form

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.