泛型方法——簡化元組的使用

通過泛型方法簡化元組的創(chuàng)建

通過泛型的類型參數(shù)推導(dǎo)判斷,再加上static方法,可以編寫出一個(gè)更方便的元組工具用于創(chuàng)建元組。

public class Tuple {
    public static <A, B> TwoTuple<A, B> tuple(A a, B b) {
        return new TwoTuple<>(a, b);
    }

    public static <A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c) {
        return new ThreeTuple<>(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);
    }

    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);
    }
}

以下的TupleTest2類是用于測(cè)試Tuple工具類的代碼。

import static com.daidaijie.generices.tuple.Tuple.tuple;

public class TupleTest2 {
    static TwoTuple<String, Integer> f() {
        return tuple("h1", 47);
    }

    static TwoTuple f2() {
        return tuple("h1", 47);
    }

    static ThreeTuple<Amphibian, String, Integer> g() {
        return tuple(new Amphibian(), "h1", 47);
    }

    static FourTuple<Vehicle, Amphibian, String, Integer> h() {
        return tuple(new Vehicle(), new Amphibian(), "h1", 47);
    }

    static FiveTuple<Vehicle, Amphibian, String, Integer, Double> k() {
        return tuple(new Vehicle(), new Amphibian(), "h1", 47, 11.1);
    }

    public static void main(String[] args) {
        TwoTuple<String, Integer> ttsi = f();
        System.out.println("ttsi = " + ttsi);
        System.out.println("f2() = " + f2());
        System.out.println("g() = " + g());
        System.out.println("h() = " + h());
        System.out.println("k() = " + k());
    }
}
// Outputs
ttsi = (h1, 47)
f2() = (h1, 47)
g() = (com.daidaijie.generices.tuple.Amphibian@1540e19d, h1, 47)
h() = (com.daidaijie.generices.tuple.Vehicle@677327b6, com.daidaijie.generices.tuple.Amphibian@14ae5a5, h1, 47)
k() = (com.daidaijie.generices.tuple.Vehicle@7f31245a, com.daidaijie.generices.tuple.Amphibian@6d6f6e28, h1, 47, 11.1)

這里要注意的是,方法f()返回的是一個(gè)參數(shù)化的TwoTuple對(duì)象,而f2()返回的是非參數(shù)化的TwoTuple對(duì)象。而在這里編譯器并沒有發(fā)出警告信息,這是因?yàn)檫@里沒有將其返回值作為一個(gè)參數(shù)化對(duì)象使用。而在某種意義上,它被"向上轉(zhuǎn)型"作為一個(gè)非參數(shù)化的TwoTuple。這個(gè)時(shí)候,如果試圖將f2()的返回值轉(zhuǎn)型為參數(shù)化的TwoTuple,編譯器就會(huì)發(fā)出警告。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2.簡單泛型 -********Java泛型的核心概念:告訴編譯器想使用什么類型, 然后編譯器幫你處理一切細(xì)節(jié) 2...
    CodingHou閱讀 432評(píng)論 0 0
  • 簡單泛型 1.泛型的主要目的之一就是用來指定容器要持有什么類型的對(duì)象,而且由編譯器來保證類型的正確性。2.Java...
    zpauly閱讀 1,323評(píng)論 0 1
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,463評(píng)論 6 13
  • object 變量可指向任何類的實(shí)例,這讓你能夠創(chuàng)建可對(duì)任何數(shù)據(jù)類型進(jìn)程處理的類。然而,這種方法存在幾個(gè)嚴(yán)重的問題...
    CarlDonitz閱讀 1,021評(píng)論 0 5
  • 當(dāng)時(shí)我父親拽著我的手臂,我已經(jīng)不在那條肥大的行動(dòng)不了了的皮船上,整個(gè)人的力量集中在與父親連接的手手相握上。而父親的...
    魏江水閱讀 258評(píng)論 0 0

友情鏈接更多精彩內(nèi)容