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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java进入编程界面_java – 编程到界面是什么意思?

發(fā)布時(shí)間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java进入编程界面_java – 编程到界面是什么意思? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

你可能正在尋找這樣的東西:

public static void main(String... args) {

// do this - declare the variable to be of type Set, which is an interface

Set buddies = new HashSet();

// don't do this - you declare the variable to have a fixed type

HashSet buddies2 = new HashSet();

}

為什么以第一種方式做好考慮呢?假設(shè)稍后你決定使用一個(gè)不同的數(shù)據(jù)結(jié)構(gòu),比如說LinkedHashSet,以便利用LinkedHashSet的功能。代碼必須如下更改:

public static void main(String... args) {

// do this - declare the variable to be of type Set, which is an interface

Set buddies = new LinkedHashSet(); //

// don't do this - you declare the variable to have a fixed type

// this you have to change both the variable type and the constructor call

// HashSet buddies2 = new HashSet(); // old version

LinkedHashSet buddies2 = new LinkedHashSet();

}

這看起來不是那么糟,對(duì)吧?但是如果你用相同的方式寫getter呢?

public HashSet getBuddies() {

return buddies;

}

這也必須改變!

public LinkedHashSet getBuddies() {

return buddies;

}

希望你看到,即使有這樣一個(gè)小程序,你對(duì)你聲明變量的類型有深遠(yuǎn)的影響。隨著對(duì)象來回傳播,這絕對(duì)有助于使程序更容易編碼和維護(hù),如果您只是依賴于聲明為接口的變量,而不是該接口的特定實(shí)現(xiàn)(在這種情況下,將其聲明為設(shè)置,不是LinkedHashSet或其他)。可以這樣:

public Set getBuddies() {

return buddies;

}

還有另一個(gè)好處,在這個(gè)(至少對(duì)我來說)差異有助于我更好地設(shè)計(jì)一個(gè)程序。但希望我的例子給你一些想法…希望它有所幫助。

總結(jié)

以上是生活随笔為你收集整理的java进入编程界面_java – 编程到界面是什么意思?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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