回顧之前學習的類庫相關知識,我們已經對其基本概念與簡單使用有了一個較為全面的了解,現在讓我們一起來結合具體的一些案例鞏固一下程式國際化和比較器的相關内容吧。
【本節目标】
通過閱讀本節内容,你将結合相關案例場景進行功能實作,複習程式國際化和比較器相關内容,進一步熟悉Locale類和ResourceBundle類,實作動态加載檔案資源類,掌握Comparable接口的實作方法,實作學生資訊的排序。
國家代碼
類庫案例分析七
編寫程式,實作國際化應用,從指令行輸入國家的代号,例如1表示中國,2表示美國,然後根據輸入代号的不同調用不同的資源檔案顯示資訊。
本程式的實作肯定要通過Locale類的對象來指定區域,随後利用ResourceBundle類加載資源檔案,而對于資料的輸入可以繼續使用初始化參數形式來完成。
-
定義中文的資源檔案:cn.mldn.message.Messages_zh_CN.properties
檔案内容如下:
info=感謝小強同學請客吃冰激淩!
- 定義英文的資源檔案:cn.mldn.message.Messages_en_US.properties
info=Thanks Qiang , your ice very nice !
- 定義程式類進行加載控制
import java.util.Locale;
import java.util.ResourceBundle;
class MessageUtil {
public static final int CHINA = 1 ;
public static final int USA = 2 ;
private static final String KEY = "info";
private static final String BASENAME = "cn.mldn.message.Messages";
public String getMessage(int num) {
Locale loc = this.getLocale(num);
if(loc == null) {
return "Nothing" ;
} else {
return ResourceBundle.getBundle(BASENAME, loc).getString(KEY);
}
}
private Locale getLocale(int num) {
switch(num) {
case CHINA:
return new Locale("zh", "CN");
case USA:
return new Locale("en", "US");
default:
return null;
}
}
}
public class JavaAPIDemo {
public static void main(String[] args) {
if (args.length != 1) { // 沒有得到輸入參數
System.out.println("程式執行錯誤,沒有設定區域編碼,正确格式:java JavaAPIDemo 選擇項")
System.exit(1);
}
int choose = Integer.parseInt(args[0]);
System.out.println(new MessageUtil().getMessage(choose));
}
}
設定輸入為1,運作結果:
感謝小強同學請客吃冰激淩!
這種在程式中不再修改的内容往往會定義成常量,這個情況在開發過程中是很常見的。
學生資訊比較
類庫案例分析八
按照“姓名:年齡:成績|姓名:年齡:成績”的格式定義字元串 “張三:21:98|李四:22:89|王五:20:70”, 要求将每組值分别儲存在Student對象之中,并對這些對象進行排序,排序的原則為:按照成績由高到低排序,如果成績相等,則按照年齡由低到高排序。
本程式最典型的做法是直接利用比較器來完成處理,如果不使用比較器也可以完成,相當于自己采用冒泡的方式進行排列,使用了比較器就可以使用Arrays類做處理。
程式編寫如下:
import java.util.Arrays;
public class JavaAPIDemo {
public static void main(String[] args) {
String input = "張三:21:98|李四:22:89|王五:20:70";
String result[] = input.split("\\|");
Student students [] = new Student [result.length];
for (int x = 0; x < result.length; x ++) {
String temp [] = result[x].split(":");
students[x] = new Student(temp[0], Integer.parseInt(temp[1]), Double.parseDouble(temp[2]));
}
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}
class Student implements Comparable<Student> {
private String name;
private int age;
private souble score;
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student stu) {
if (this.score < stu.score) {
return 1;
} else if (this.score > stu.score) {
return -1;
} else {
return this.age - stu.age;
}
}
@Override
public String toString() {
return "【學生資訊】姓名:" + this.name + "、年齡:" + this.age + "、成績:" + this.score + "\n";
}
}
運作結果:
[【學生資訊】姓名:張三、年齡:21、成績:98.0
, 【學生資訊】姓名:李四、年齡:22、成績:89.0
, 【學生資訊】姓名:王五、年齡:20、成績:70.0
]
結構化字元串處理:“内容|内容|”,如果有複雜的情況内容裡面可能再有其他标記。這種做法在開發中是廣泛使用的。
想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。
本内容視訊來源于
阿裡雲大學 下一篇:Flie類-踏入檔案的領域 | 帶你學《Java語言進階特性》之四十六 更多Java面向對象程式設計文章檢視此處