java-泛型类和方法

简单的类泛型

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)


    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值