DAY15-------Generics in the Java language

Source: Internet
Author: User

DAY15-------Generics in the Java language

First, generic overview:

The concept of "generics" may be a little strange to you, but it's not, you can understand it as a variety of data types, variable data types, data types at different requirements, and so on. In development, generics are used very often, including both generic classes and generic methods. Mastering the relationship between them is a great help to you in the logical framework. Let's take a look at what the generics are all about.


Second, the generic type "Generic"

is to define a class without defining the relevant data type, and then define the relevant data type when we need to use the class.

For eight basic data types, each data type defines a wrapper class that corresponds to the encapsulation.

Byte;short;int;long;float;double;char;boolean---> This is the basic data type

Byte;short;integer; Long; float;double; Character; Boolean---> Corresponding wrapper class


1, define a generic: generic definition is actually very simple. Adding a brick symbol and data type after the class name of a class is a generic type defined. For example

Class student<t>{}

This "<T>" is an arbitrary generic type, which I define myself. And this is his structure. I have also spoken in the example.


2. Set the Get and set methods to access private member variables

Get method: In the Java language this you have to understand, is actually a special case of the private access descriptor permissions, so to speak! When the private specifier modifies a member variable, the member variable becomes privatized, and the other member variable cannot be used. When a Get method is used, the member variable can be fetched, read, and not private.

Set method: Same as the Get method. is a break-up of the private specifier, which is able to modify and change the member variables of the privatization by the set method.

A little more popular: just as a room was closed, we carved a door on the wall. We can get things out of the room through this door, and we can put things in the room as well.

Do you understand me?


3. Define multiple generics: definition of generics we can define multiple types directly, and we can define more than one at a time after the class name. As follows:

Class student<x,y,z>{}

4. Define a generic method: This means that in a class you can also define a generic release, such as

Class student<x,y,z>{public <T> void display () {//**********}}


Iii. Concrete examples of integration

PACKAGE&NBSP;WWW.COM.C2;//1, the definition of a generic class class student01<t>{//t is the type of this class, but can be created below the object when the relevant data type to define generics can! Private member: After privatization it is not possible to manipulate this variable directly, but after setting the relevant get and set methods, you can set the get accessor directly using the variable private t name;//public t  GetName ()  {return name;} Set accessor Public void setname (T name)  {this.name = name;}} 2. Normal class use Get and set method access to set private member class student02{//Private member: After privatization, you cannot manipulate this variable directly, but after you have set the relevant get and set methods, you can use the variable private  string name;//set the Get Accessor public string getname ()  {return name;} Set accessor Public void setname (String name)  {this.name = name;}} 3. Define multiple generics class student03<x,y>{//This is a generic class//defined private variable private x name;private y age;// Generates the corresponding get and set methods. Note that this method is a generic method public x getname ()  {return name;} Public void setname (X name)  {this.name = name;} Public y getage ()  {return age;} Public void setage (y age)  {this.agE = age;}} 4. Define the generic method Class student04{public <t> void display (T a) {System.out.println (A +) This is the definition of a generic method! ");}} This is the main class Public class generic {public static void main (String[] args)  {/ /-----------instance using generic--------student01<object> stu1 = new student01<object> (); The  //object class is the parent class for all classes. All data types can be defined continuously. However, if this object is changed to a wrapper class corresponding to the base data type, only this data type can be used Stu1.setname (001);//This is the integer type used stu1.setname (10.0f); This is the floating-point type Stu1.setname ("dzx01") used,//This is the string type System.out.println (Stu1.getname ()) used,//----------- The instance uses get and set access to the--------student02 stu2 = new student02 (); Stu2.setname ("dzx02");// Set the value System.out.println (Stu2.getname ()) by the set method, or get to//-----------instance using multiple generic--------student03<string through the Get method, Integer> stu3 = new student03<string,integer> ();//This is the time to define the appropriate data type! This will ensure the security of the data Stu3.setname ("dzx03"); Stu3.setage (20); System.out.println (Stu3.getname ()); System.out.println (STU3. Getage ());//-----------instance uses a generic method--------student04 stu4 = new student04 (); Stu4.display (" Display "),//depending on the value you pass will automatically determine what data type}}


Iv. Concluding remarks:

You should know what generics are here, right? Learning generics is important to you when you learn to assemble. So you first understand the example I wrote it! It's crucial that you'll understand when you're thoroughly understand.

This article from the "Program Ape" blog, reproduced please contact the author!

DAY15-------Generics in the Java language

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.