Java泛型多种类型参数

翻译 ww9992019com 发表于 5 天前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="10975">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>泛型类可以有多个类型参数。以下示例将展示上述概念。</p>

<h2 id="h2-u793Au4F8B"><a name="%E7%A4%BA%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>示例</h2><p>使用您喜欢的编辑器创建以下java程序,并保存到文件:<em>MultipleTypeParameters.java</em> 中,代码如下所示 -</p>

<pre><code class="lang-java">package com.yiibai;

public class MultipleTypeParameters {

public static void main(String[] args) {

    Box3&lt;Integer, String&gt; box = new Box3&lt;Integer, String&gt;();

    box.add(Integer.valueOf(103), "Hello World");

    System.out.printf("Integer Value :%d\n", box.getFirst());

    System.out.printf("String Value :%s\n", box.getSecond());



    Box3&lt;String, String&gt; box1 = new Box3&lt;String, String&gt;();

    box1.add("Message", "Hello World");

    System.out.printf("String Value :%s\n", box1.getFirst());

    System.out.printf("String Value :%s\n", box1.getSecond());

}

}

class Box3&lt;T, S&gt; {

private T t;

private S s;



public void add(T t, S s) {

    this.t = t;

    this.s = s;

}



public T getFirst() {

    return t;

}



public S getSecond() {

    return s;

}

}

</code></pre>

<p>执行上面代码,得到以下结果 - </p>

<pre><code class="lang-shell">Integer Value :103

String Value :Hello World

String Value :Message

String Value :Hello World

</code></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Java泛型方法"><i class="layui-icon"></i>
                            Java泛型方法</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Java泛型参数化类型"><i class="layui-icon"></i>
                            Java泛型参数化类型</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复