泛型的嵌套使用

package com.j2se;
/**
 * 示例:泛型的嵌套设置
 * @author Administrator
 *
 */
public class GenericsDemo6 {
public static void main(String[] args) {
Domain> d = null;  //指定任意类型数据
Info1 i1 = new Info1("刘亦非", 20); 
d =new Domain>(i1);
   System.err.println(d.getInfo().getAge());
System.out.println(d.getInfo().getName());

Info1 fav = new Info1("刘亦菲", "自称年龄 16岁");    //指定任意类型数据
Domain> dn = new Domain>(fav);
System.err.println(dn);
}
}


class Info1{
private T name;
private V age;
public Info1 (T name, V age){
this.name = name;
this.age =age;
}
public T getName() {
return name;
}
public void setName(T name) {
this.name = name;
}
public V getAge() {
return age;
}
public void setAge(V age) {
this.age = age;
}
@Override
public String toString() {
return "爆料  [name=" + name + ", age=" + age + "]";
}


}
 class Domain{
private S info;
public Domain( S info){
this.info = info;
}
public S getInfo() {
return info;
}
public void setInfo(S info) {
this.info = info;
}
@Override
public String toString(){
return this.info.toString();
}
 

 }

//结果

/**20刘亦非


爆料  [name=刘亦菲, age=自称年龄 16岁]**/

你可能感兴趣的:(Java基础程序设计)