简单的类泛型
class TwoTuple<A,B>{
public final A first;
public final B second;
public TwoTuple(A a, B b){
first = a;
second = b;
}
public String toString(){
return "(" + first + " , " + second + ")";
}
}
class ThreeTuple<A, B, C> extends TwoTuple<A, B>{
public final C third;
public ThreeTuple(A a, B b, C c){
super(a, b);
third = c;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ ")";
}
}
class FourTuple<A, B, C, D> extends ThreeTuple<A, B, C> {
public final D fourth;
public FourTuple(A a, B b, C c, D d){
super(a, b, c);
fourth = d;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ " , "+ fourth+ ")";
}
}
class FiveTuple<A, B, C, D, E> extends FourTuple<A, B, C, D> {
public final E fifth;
public FiveTuple(A a, B b, C c, D d, E e){
super(a, b, c ,d);
fifth = e;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ " , "+ fourth+ " , "+ fifth+")";
}
}
class Amphibian{}
class Vehicle{}
public class Main{
static TwoTuple<String, Integer>f(){
return new TwoTuple<String, Integer>("hi", 47);
}
static ThreeTuple<Amphibian, String ,Integer> g(){
return new ThreeTuple<Amphibian,String, Integer>(new Amphibian(), "hi", 47);
}
static FourTuple<Vehicle, Amphibian, String ,Integer> h(){
return new FourTuple<Vehicle, Amphibian, String ,Integer>(new Vehicle(), new Amphibian(), "hi", 47);
}
static FiveTuple<Vehicle, Amphibian, String ,Integer, Double> k(){
return new FiveTuple<Vehicle, Amphibian, String ,Integer, Double>(new Vehicle(), new Amphibian(), "hi", 47, 11.1);
}
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
TwoTuple<String, Integer> ttsi = f();
System.out.println(g());
System.out.println(h());
System.out.println(k());
}
}
运行结果:
(com.company.Amphibian@135fbaa4 , hi , 47)
(com.company.Vehicle@45ee12a7 , com.company.Amphibian@330bedb4 , hi , 47)
(com.company.Vehicle@2503dbd3 , com.company.Amphibian@4b67cf4d , hi , 47 , 11.1)
泛型方法
class GenericMethods{
public <T> void f(T x){
System.out.println(x.getClass().getName());
}
}
public class Main{
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
GenericMethods gm = new GenericMethods();
gm.f("");
gm.f(1);
gm.f(1.0);
gm.f(1.0F);
gm.f('c');
gm.f(gm);
}
}
运行结果:
java.lang.String
java.lang.Integer
java.lang.Double
java.lang.Float
java.lang.Character
com.company.GenericMethods
泛型方法调用时,不用明确指出参数类型,基本类型会被自动打包
利用'类型参数推断'来实现第一个例子
class TwoTuple<A,B>{
public final A first;
public final B second;
public TwoTuple(A a, B b){
first = a;
second = b;
}
public String toString(){
return "(" + first + " , " + second + ")";
}
}
class ThreeTuple<A, B, C> extends TwoTuple<A, B>{
public final C third;
public ThreeTuple(A a, B b, C c){
super(a, b);
third = c;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ ")";
}
}
class FourTuple<A, B, C, D> extends ThreeTuple<A, B, C> {
public final D fourth;
public FourTuple(A a, B b, C c, D d){
super(a, b, c);
fourth = d;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ " , "+ fourth+ ")";
}
}
class FiveTuple<A, B, C, D, E> extends FourTuple<A, B, C, D> {
public final E fifth;
public FiveTuple(A a, B b, C c, D d, E e){
super(a, b, c ,d);
fifth = e;
}
public String toString(){
return "(" + first + " , " + second + " , "+ third+ " , "+ fourth+ " , "+ fifth+")";
}
}
class Tuple{
public static <A, B> TwoTuple<A, B> tuple(A a, B b){
return new TwoTuple<A, B>(a, b);
}
public static <A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c){
return new ThreeTuple<A, B, C>(a ,b, c);
}
public static <A, B, C, D> FourTuple<A, B, C, D> tuple(A a, B b,C c, D d){
return new FourTuple<A, B, C, D>(a , b, c, d);
}
public static <A, B, C, D, E> FiveTuple<A, B, C, D, E> tuple(A a, B b,C c, D d, E e){
return new FiveTuple<A, B, C, D, E>(a , b, c, d, e);
}
}
class Amphibian{}
class Vehicle{}
public class Main{
static TwoTuple<String, Integer> f(){
return Tuple.tuple("hi", 47);
}
static TwoTuple f2(){
return Tuple.tuple("hi", 47);
}
static ThreeTuple<Amphibian, String, Integer> g(){
return Tuple.tuple(new Amphibian(), "hi", 47);
}
static FourTuple<Vehicle, Amphibian, String, Integer> h(){
return Tuple.tuple( new Vehicle(), new Amphibian(), "hi", 47);
}
static FiveTuple<Vehicle, Amphibian, String, Integer, Double> k(){
return Tuple.tuple( new Vehicle(), new Amphibian(), "hi", 47, 11.1);
}
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
TwoTuple<String, Integer> ttsi = f();
System.out.println(ttsi);
System.out.println(f2());
System.out.println(g());
System.out.println(h());
System.out.println(k());
}
}
运行结果:
(hi , 47)
(hi , 47)
(com.company.Amphibian@135fbaa4 , hi , 47)
(com.company.Vehicle@45ee12a7 , com.company.Amphibian@330bedb4 , hi , 47)
(com.company.Vehicle@2503dbd3 , com.company.Amphibian@4b67cf4d , hi , 47 , 11.1)