Sometimes classes and methods need to constrain type variables. For example, you have a method that you just want to receive a specific type and its subtypes as a parameter.
The following is a sample of a method that defines the type of the receiving parameter to illustrate how to qualify a type variable.
First, there are a few simple helper classes:
Package Generic;public class Person extends Animal {private string name;public person (String name) {super (); this.name = NA Me;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "person [name=" + name + "]";}}
Package Generic;public class Student extends person {private String studentnumber;public Student (string name, String regis Trationnumber) {super (name); this.studentnumber = Registrationnumber;} Public String Getstudentnumber () {return studentnumber;} public void Setstudentnumber (String studentnumber) {this.studentnumber = Studentnumber;} @Overridepublic String toString () {return "Student [studentnumber=" + Studentnumber + ", name=" + getName () + "]";}}
Package Generic;public class Teacher extends person {private String teachernumber;public Teacher (string name, string teach Ernumber) {super (name);//TODO auto-generated constructor stubthis.teachernumber = Teachernumber;} Public String Getteachernumber () {return teachernumber;} public void Setteachernumber (String teachernumber) {this.teachernumber = Teachernumber;} @Overridepublic String toString () {return "Teacher [teachernumber=" + Teachernumber + ", name=" + getName () + "]";}}
The following is a test class that includes a method that restricts the type of receive parameters:
package Generic;public class Test {public static void main (string[] args) {//TODO Auto-gen erated method Stubperson person = new Person ("Li"); Student Student = new Student ("Yuncong", "1"); Teacher Teacher = new Teacher ("Wangfang", "99"); Building Building = new Building ("Qiuyuan");//test1/** * Test1 indicates that the parameters of the Getpersoninfo (T person) method can only be instances of person * and its subclasses; */syst Em.out.println (Test.getpersoninfo (person)); System.out.println (Test.getpersoninfo (student)); System.out.println (Test.getpersoninfo (teacher));/** * Bound mismatch:the generic method Getpersoninfo (T) of type Test is * Not applicable for the arguments (Building). The inferred type * Building is not a valid substitute for the bounded parameter <t * extends person> *///System.ou T.println (Test.getpersoninfo (building));//Error}/* * The Qualified type of T determines which methods of the instance of T can be called in the method. * * @param person * @return */public static <t extends person> String getpersoninfo (T t) {return t.tostring ();}}
the results of the test class are performed such as the following:
person [Name=li]
Student [Studentnumber=1, Name=yuncong]
Teacher [teachernumber=99, Name=wangfang]
The qualification of a type variable is only extends this one keyword,extends can be connected to multiple interface super-types, but at most one class, assuming extends followed by a class, this class must be the first one. Multiple qualifying types are separated by &.
such as: T extends Person & comparable<t>.
Limitations of Java generic type variables