Sometimes classes and methods need to constrain type variables, such as you have a method that you only want to receive a particular type and its subtypes as arguments.
Here's an example of how to qualify a type variable with a method that qualifies the type of the receiving parameter.
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 contains a method that qualifies the type of the receive parameter:
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; */syste M.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 result of running the test class is as follows:
person [Name=li]
Student [Studentnumber=1, Name=yuncong]
Teacher [teachernumber=99, Name=wangfang]
The qualification of a type variable is only extends this keyword, extends can be connected to multiple interface super-types, but at most one class, if extends followed by a class, the 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