Java Learning Note Nine (generics)

Source: Internet
Author: User

1. Introduction

The so-called generics are passing types as a parameter, and with generics the type is no longer immutable and can be specified by generic parameters. can provide flexibility in program development.


2. Use of generic classes or interfaces

When a generic class declaration is not much different from a normal class, only the declaration of a type variable is added to the class

public class class name < type variable >{..... ..... ................ ....................... ........}

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com.fanxing;//a custom generic class myvalue<t,u>{//defines two member variables of the type specified by the type variable private T value1;private U value2;// Defines a set method of two member variables whose parameter type is determined by the type variable public void SetValue (T newvalue1,u newValue2) {this.value1=newvalue1;this.value2= NewValue2;} The Get method of the value1 member variable public T getValue1 () {return this.value1;} The Get method of the value2 member variable public U getValue2 () {return this.value2;}} Main class public class Sample35_1{public static void Main (String args[]) {//Create a custom generic class object and specify two type variables Myvalue<string,integer > Mv1=new myvalue<string,integer> ();//Call the Set method to set the member variable Mv1.setvalue ("Hello this parameter is String type", New Integer (100));// Returns the value of the first member variable, string str=mv1.getvalue1 ();//Returns the value of the second member variable integer num=mv1.getvalue2 ();//Prints two values System.out.println (" The first parameter value of the Mv1 object is: "+str+" the second parameter value is: "+num+". ");//Create an object of another custom generic class and specify a different two type variable myvalue<integer,string> mv2=new myvalue<integer,string> ();// Call the Set method to set the member variable Mv2.setvalue (new Integer (100), "hello the parameter is a string type"),//To return the value of the first member variable num=mv2.getvalue1 ();//Returns the value of the second member variable str= Mv2.getvalUe2 ();//Print two values System.out.println (the first parameter value of the Mv2 object is: "+num+" the second parameter value is: "+str+". ");}} </span>


The above example customizes a generic class myvalue, has two type parameters T and U, and the member variable is also specified by the type parameter. Next, when you create an object of the generic class myvalue, you specify an explicit type value for the type variable.


3. Generic Methods

Generic methods are also supported in Java, and are more flexible than generic classes.

public static < type variable > return type method name (parameter list) {}

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package Com.fanxing;public class sample35_2{public <T> void Printtest (T t1) {System.out.println (T1);} The custom generic method public static <T> T GetLast (t[] a) {//returns the last element of the specified type array return a[a.length-1];} public static void Main ( String args[]) {//Create an array of string types string[] str={"Here is the first element", "here is the second element", "here is the third element"};//uses the generic method and assigns it a type parameter string laststr= Sample35_2.<string>getlast (str);//Print result System.out.println ("The last element of the String array str is:" "+laststr+" ". ");//Create an array of type integer[] num={new integer, new integer, new Integer (300)};//uses the generic method, but does not explicitly specify the type parameter integer Lastnum=sample35_2.getlast (num); Sample35_2 a=new Sample35_2 () a.printtest ("Hello");//Print result System.out.println ("The last element of the integer array num is:" "+lastnum+" ".) ");}} </span>

The above is the use of generic methods, generally generic methods are mostly static methods .



4. Limitations of type Variables

If you want to limit the type variable, then you need to use the restriction of the type variable, the specific syntax is as follows

Type variable extends qualified type sequence

A sequence of qualifying types, which can be classes, interfaces, and so on, between multiple restrictions through &

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package Com.fanxing;import java.io.*;//defines a generic class and qualifies the type parameter class Myvalue1<t extends number&comparable& serializable>{//defines a member variable of the type specified by the type parameter private T value;//member variable of the Set method public void SetValue (T newvalue) {This.value=newvalue ;} Get method for member variable public T GetValue () {return this.value;}} Main class public class Sample35_3{public static void Main (String args[]) {//create type parameter specified as Integer myvalue object Myvalue1<integer > Mv=new myvalue1<integer> ();//Set the value in MV Mv.setvalue (new Integer (200));//Get the value in MV Integer num=mv.getvalue ();// Print the value System.out.println ("The value in the MV object is:" +num+ "This value belongs to both the number class and comparable and serializable.) ");}} </span>

The above example is the qualification of the type variable, limited to number and serializable




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Learning Note Nine (generics)

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.