日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

微型技术博客之匿名内部类

發(fā)布時(shí)間:2023/12/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微型技术博客之匿名内部类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這學(xué)期選修了安卓手機(jī)平臺(tái)開(kāi)發(fā)課程,開(kāi)發(fā)使用的是java語(yǔ)言,所以在這個(gè)過(guò)程中我又鞏固了很多java的知識(shí)點(diǎn),我記得在前年下半年的時(shí)候我學(xué)了java這門(mén)課程,但是后來(lái)有很長(zhǎng)一段時(shí)間沒(méi)有寫(xiě)java程序了,導(dǎo)致有很多語(yǔ)法什么之類(lèi)的慢慢淡忘,現(xiàn)在借此機(jī)會(huì),又慢慢的一點(diǎn)點(diǎn)的撿拾起來(lái)了。今天我的這篇微型技術(shù)博客探討的就是java里面的匿名內(nèi)部類(lèi)。

1.匿名內(nèi)部類(lèi)其實(shí)就是內(nèi)部類(lèi)的簡(jiǎn)寫(xiě)格式。

2..定義匿名內(nèi)部類(lèi)的前提:內(nèi)部類(lèi)必須是繼承一個(gè)抽象類(lèi)或者實(shí)現(xiàn)接口。只要一個(gè)類(lèi)是抽象的或是一個(gè)接口,那么其子類(lèi)中的方??? 法都可以使用匿名內(nèi)部類(lèi)來(lái)實(shí)現(xiàn)

3.匿名內(nèi)部類(lèi)的格式:? new 父類(lèi)或者接口(){定義子類(lèi)的內(nèi)容}

4.其實(shí)匿名內(nèi)部類(lèi)就是一個(gè)匿名子類(lèi)對(duì)象

5.使用匿名內(nèi)部類(lèi)我們必須要繼承一個(gè)父類(lèi)或者實(shí)現(xiàn)一個(gè)接口,當(dāng)然也僅能只繼承一個(gè)父類(lèi)或者實(shí)現(xiàn)一個(gè)接口。同時(shí)它也是沒(méi)有class關(guān)鍵字,這是因?yàn)槟涿麅?nèi)部類(lèi)是直接使用new來(lái)生成一個(gè)對(duì)象的引用。當(dāng)然這個(gè)引用是隱式的。

6.注意點(diǎn):

? ? ? 匿名內(nèi)部類(lèi)中是不能定義構(gòu)造函數(shù)的。

????? 匿名內(nèi)部類(lèi)中不能存在任何的靜態(tài)成員變量和靜態(tài)方法。

????? 匿名內(nèi)部類(lèi)為局部?jī)?nèi)部類(lèi),所以局部?jī)?nèi)部類(lèi)的所有限制同樣對(duì)匿名內(nèi)部類(lèi)生效。

????? 匿名內(nèi)部類(lèi)不能是抽象的,它必須要實(shí)現(xiàn)繼承的類(lèi)或者實(shí)現(xiàn)的接口的所有抽象方法

1匿名內(nèi)部類(lèi)的實(shí)現(xiàn)
?
abstractclass Time{
? ? publicabstractvoid starttime();
}
?
publicclass Date{
? ? publicstaticvoid main(String[] args) {
? ? ? ? ?Time t =new Time(){
? ? ? ? ? ? publicvoid starttime() {
? ? ? ? ? ? ? ? System.out.println("the time used is:");
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? t.starttime();
? ? }
}
?
在接口上使用匿名內(nèi)部類(lèi)
interface Time {
? ? publicvoid starttime();
}
publicclass Date{
? ? publicstaticvoid main(String[] args) {
? ? ? ? ?Time t =new Time(){
? ? ? ? ? ? publicvoid starttime() {
? ? ? ? ? ? ? ? System.out.println("the time used is:");
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? t.starttime();
? ? }
}
?
?多線(xiàn)程實(shí)現(xiàn)匿名內(nèi)部類(lèi)
publicclass Sum{
? ? publicstaticvoid main(String[] args) {
? ? ? ? Thread s= new Thread() {
publicvoid count() {
? i=1;
?S=0;
? ? ? ? ? ? ? ? while(i<=100) {
? ? ? ? ? ? ? ? ?s=s+i;
? ? ? ? ? ? ? ? ?i++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
}
System.out.print(s);
?
? ? ? ? };
? ? ? ? s.start();
? ? }
}
?
Runnable接口內(nèi)部類(lèi)實(shí)現(xiàn)
publicclass Sum{
? ? publicstaticvoid main(String[] args) {
? ? ? ? Runnabler = new Runnable(){
publicvoid count() {
?i=1;
?S=0;
? ? ? ? ? ? ? ? while(i<=100) {
? ? ? ? ? ? ? ? ?s=s+i;
? ? ? ? ? ? ? ? ?i++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
}
System.out.print(s);
?
? ? ? ? };
? ? ? ? s.start();


publicclass TestAnonymousInterClass{ ?
? ? publicstaticvoid main(String args[]){ ?
? ? ? ? TestAnonymousInterClass test=new TestAnonymousInterClass(); ?
? ? ? ? test.show(); ?
? ? } ?
? ? //在這個(gè)方法中構(gòu)造了一個(gè)匿名內(nèi)部類(lèi) ?
? ? privatevoid show(){ ?
? ? ? ? Out anonyInter=new Out(){// 獲取匿名內(nèi)部類(lèi)實(shí)例 ?
? ? ? ? ? ? ??
? ? ? ? ? ? void show(){//重寫(xiě)父類(lèi)的方法 ?
? ? ? ? ? ? ? ? System.out.println("this is Anonymous InterClass showing."); ?
? ? ? ? ? ? } ?
? ? ? ? }; ?
? ? ? ? anonyInter.show();// 調(diào)用其方法 ?
? ? } ?
} ??
?
// 這是一個(gè)已經(jīng)存在的類(lèi),匿名內(nèi)部類(lèi)通過(guò)重寫(xiě)其方法,將會(huì)獲得另外的實(shí)現(xiàn) ?
class Out{ ?
? ? void show(){ ?
? ? ? ? System.out.println("this is Out showing."); ?
? ? } ?
}?





總結(jié)

以上是生活随笔為你收集整理的微型技术博客之匿名内部类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。