一、泛型的扩展
1、先定义一个父类
public class Parent<T1, T2>
{
private T1 foo1;
private T2 foo2;
public T1 getFoo1()
{
return foo1;
}
public void setFoo1(T1 foo1)
{
this.foo1 = foo1;
}
public T2 getFoo2()
{
return foo2;
}
public void setFoo2(T2 foo2)
{
this.foo2 = foo2;
}
}
2、子类扩展父类,并增加一个泛型
public class Child<T1, T2, T3> extends Parent<T1, T2>
{
private T3 foo3;
public T3 getFoo3()
{
return foo3;
}
public void setFoo3(T3 foo3)
{
this.foo3 = foo3;
}
}
二、 泛型的继承
1、先定义一个接口
public interface ParentInterface<T1, T2>
{
public void setFoo1(T1 t1);
public void setFoo2(T2 t2);
public T1 getFoo1();
public T2 getFoo2();
}
2、子类继承父接口
public class ChildClass<T1, T2> implements ParentInterface<T1, T2>
{
private T1 foo1;
private T2 foo2;
/**
* 实现接口中的四个方法
*/
@Override
public void setFoo1(T1 t1)
{
this.foo1 = t1;
}
@Override
public void setFoo2(T2 t2)
{
this.foo2 = t2;
}
@Override
public T1 getFoo1()
{
return this.foo1;
}
@Override
public T2 getFoo2()
{
return this.foo2;
}
}