/***@authorRollen-Holt 使用泛型*/
class hello{
hello(){
}publicT getName(){returnname;
}public voidsetName(T name){this.name =name;
}publicV getAge(){returnage;
}public voidsetAge(V age){this.age =age;
}privateT name;privateV age;public static voidmain(String[] args){
hello he = new hello();
he.setAge(10);
he.setName("Rollen Holt");
System.out.println(he.getName()+ " " +he.getAge());
}
}/***@authorRollen-Holt 泛型类的构造方法定义*/
class hello{
hello(T name, V age){this.age =age;this.name =name;
}publicT getName(){returnname;
}publicV getAge(){returnage;
}privateT name;privateV age;public static voidmain(String[] args){
hello he=new hello("Rollen",12);
System.out.println(he.getName()+" "+he.getAge());
}
}/***@authorRollen-Holt 使用通配符*/
class info{
info(T name){this.name =name;
}privateT name;
}classhello{public static void function(info>temp){
System.out.println("内容: "+temp);
}public static voidmain(String[] args){
info demo=new info("Rollen");
function(demo);
}
}/***@authorRollen-Holt 泛型上限*/
class info{
info(T age){this.age=age;
}privateT age;
}classhello{public static void function(info extends Number>temp){
System.out.println("内容"+temp);
}public static voidmain(String[] args){
info demo=new info(1);
function(demo);
}
}/***@authorRollen-Holt 泛型下限*/
class info{
info(T age){this.age=age;
}privateT age;
}classhello{public static void function(info super String>temp){
System.out.println("内容"+temp);
}public static voidmain(String[] args){//此处只能使用String 或者Object
info demo=new info("Rollen");
function(demo);
}
}/***@authorRollen-Holt 泛型和子类继承的限制*/
class info{
}classhello{public static voidmain(String[] args){
info demo1=new info();
info demo2=new info();//demo2=demo1; 此处错误
}
}/*** 上面的例子说明,一个类的子类可以通过多态性被其父类实例化
* 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。*/如果允许上面的条语句的话,会出现:
Exception in thread"main"java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from info to infoat hello.main(hello.java:12)
泛型接口的两种实现:/***@authorRollen-Holt 泛型接口的实现1*/
interface info{public voidsay();
}//直接在子类之后声明泛型
class hello implements info{public static voidmain(String[] args){
info demo = new hello();
demo.say();
}public voidsay(){
System.out.println("hello");
}
}/***@authorRollen-Holt 泛型接口的实现2*/
interface info{public voidsay();
}//在子类实现的接口中明确给出泛型类型
class hello implements info{public static voidmain(String[] args){
info demo = newhello();
demo.say();
}public voidsay(){
System.out.println("hello");
}
}/***@authorRollen-Holt 使用泛型通一传入参数的类型*/
class info{privateT var;publicT getVar(){returnvar;
}public voidsetVar(T var){this.var =var;
}publicString toString(){return this.var.toString();
}
}classhello{public static voidmain(String[] args){
info demo1=new info();
info demo2=new info();
demo1.setVar("Rollen");
demo2.setVar("Holt");
printAdd(demo1, demo2);
}//此处传递的都是同一个String类型的
public static void printAdd(info demo1, infodemo2){
System.out.println(demo1.getVar()+" "+demo2.getVar());
}
}
否则的话如下所示:出现错误:/***@authorRollen-Holt 使用泛型通一传入参数的类型*/
class info{privateT var;publicT getVar(){returnvar;
}public voidsetVar(T var){this.var =var;
}publicString toString(){return this.var.toString();
}
}classhello{public static voidmain(String[] args){
info demo1=new info();
info demo2=new info();
demo1.setVar("Rollen");
demo2.setVar(30);//此处调用错误
printAdd(demo1, demo2);
}//此处传递的都是同一个String类型的
public static void printAdd(info demo1, infodemo2){
System.out.println(demo1.getVar()+" "+demo2.getVar());
}
}
Exception in thread"main"java.lang.Error: Unresolved compilation problem:
The method printAdd(info, info) in the type hello is not applicable for the arguments (info, info)
at hello.main(hello.java:26)