加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

class和 class类型 有什么区别

发布时间:2020-12-14 06:38:05 所属栏目:Java 来源:网络整理
导读:转自: pre class="best-text mb-10" style="color:rgb(51,51,51);font-size:14px;"平时看java源代码的时候,如果碰到泛型的话,我想? T K V E这些是经常出现的,但是有时想不起来代表什么意思,今天整理下: ? 表示不确定的java类型。 T 表示java类型。 K

转自:

<pre class="best-text mb-10" style="color:rgb(51,51,51);font-size:14px;">平时看java源代码的时候,如果碰到泛型的话,我想? T K V E这些是经常出现的,但是有时想不起来代表什么意思,今天整理下: ? 表示不确定的java类型。 T 表示java类型。 K V 分别代表java键值中的<a class="baidu-highlight" href="https://www.baidu.com/s?wd=Key&amp;tn=44039180_cpr&amp;fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuy7hPAN-P19bryn3uymv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjn4rjTvPjfz" rel="nofollow" style="color:rgb(0,0);">Key Value。 E 代表Element。 Object跟这些东西代表的java类型有啥区别呢? Object是所有类的根类,是具体的一个类,使用的时候可能是需要类型强制转换的,但是用T ?等这些的话,在实际用之前类型就已经确定了,不需要强制转换。
<div id="replyask-58260231" class="replyask line replyask-ask" style="color:rgb(51,51);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;">
<div class="ask f-14 grid">追问:
<div id="replyask-content-58260231" class="replyask-content line ml-10">

也就是说,这个方法能知道返回的是哪种类型(父类),就用T行了?如果完全不知道的就用?用T的得到的对象就不需要类型转换了,而用?的就必需用强转了!
第一种是固定的一种泛型,第二种是只要是Object类的子类都可以,换言之,任何类都可以,因为Object是所有类的根基类固定的泛型指类型是固定的,比如:Interge,String. 就是<? extends Collection> 这里?代表一个未知的类型,但是,这个未知的类型实际上是Collection的一个子类,Collection是这个通配符的上限.举个例子class Test { }其中,限定了构造此类实例的时候T是一个确定类型(具体类型),这个类型实现了Collection接口,但是实现 Collection接口的类很多很多,如果针对每一种都要写出具体的子类类型,那也太麻烦了,干脆还不如用Object通用一下。其中,?是一个未知类型,是一个通配符泛型,这个类型是实现Collection接口即可。_________________________上面讲的是什么鬼,当你知道引入通配符泛型的由来之后(下面代码由java1234.com提供)_________________________________________________________________________________________ The method take(Animal) in the type Test is not applicable for the arguments (Demo)The method take(Animal) in the type Test is not applicable for the arguments (Demo)The method take(Animal) in the type Test is not applicable for the arguments (Demo) 当引入泛型之后,遇到这种情况,参数怎么写都不适合,总有2个方法不适用,为了给泛型类写一个通用的方法,这时候就需要引入了 ?通配符的概念。
  Demo  Animal></span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;private</span><span style="line-height:1.5 !important;"&gt; T ob;

</span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;public</span><span style="line-height:1.5 !important;"&gt; T getOb() {
    </span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;return</span><span style="line-height:1.5 !important;"&gt; ob;
}

</span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;void</span><span style="line-height:1.5 !important;"&gt; setOb(T ob) {
    </span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;this</span>.ob =<span style="line-height:1.5 !important;"&gt; ob;
}

</span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;public</span><span style="line-height:1.5 !important;"&gt; Demo(T ob) {
    </span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;super</span><span style="line-height:1.5 !important;"&gt;();
    </span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;this</span>.ob =<span style="line-height:1.5 !important;"&gt; ob;
}

</span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;void</span><span style="line-height:1.5 !important;"&gt; print(){
    System.out.println(</span>"T的类型是:"+<span style="line-height:1.5 !important;"&gt;ob.getClass().getName());
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读