package com.j2se;
/**
* 示例:泛型的嵌套设置
* @author Administrator
*
*/
public class GenericsDemo6 {
public static void main(String[] args) {
Domain<Info1<String, Integer>> d = null; //指定任意类型数据
Info1<String, Integer> i1 = new Info1<String, Integer>("刘亦非", 20);
d =new Domain<Info1<String,Integer>>(i1);
System.err.println(d.getInfo().getAge());
System.out.println(d.getInfo().getName());
Info1<String, String> fav = new Info1<String, String>("刘亦菲", "自称年龄 16岁"); //指定任意类型数据
Domain<Info1<String, String>> dn = new Domain<Info1<String,String>>(fav);
System.err.println(dn);
}
}
class Info1<T, V>{
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<S>{
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();
}
/**
* 示例:泛型的嵌套设置
* @author Administrator
*
*/
public class GenericsDemo6 {
public static void main(String[] args) {
Domain<Info1<String, Integer>> d = null; //指定任意类型数据
Info1<String, Integer> i1 = new Info1<String, Integer>("刘亦非", 20);
d =new Domain<Info1<String,Integer>>(i1);
System.err.println(d.getInfo().getAge());
System.out.println(d.getInfo().getName());
Info1<String, String> fav = new Info1<String, String>("刘亦菲", "自称年龄 16岁"); //指定任意类型数据
Domain<Info1<String, String>> dn = new Domain<Info1<String,String>>(fav);
System.err.println(dn);
}
}
class Info1<T, V>{
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<S>{
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岁]**/