一、自定義異常
1)所謂自定義異常,通常就是定義了一個繼承自Exception類的子類,那麼這個類就是一個自定義異常類;通常情況下我們都會直接繼承Exception類,一般不會繼承自某個運行時的異常類。
2)我們可以使用多個catch塊來捕獲異常,這時需要將父類型的catch塊放到子類型的catch塊之後,這樣才能保證後續的catch可能被執行,否則子類型的catch將永遠無法到達,Java編譯器會報錯誤;如果多個catch塊的異常類型是獨立的,那麼誰前誰後都可以。
3)如果try塊中存在return語句,那麼首先也需要將finally塊中的代碼執行完畢,然後再返回;如果try塊中存在System.exit(0)語句,那麼就不會執行finally塊中代碼,因爲System.exit(0)會終止當前運行的java虛擬機,程序會在虛擬機終止前停止執行。
二、GUI(Graphical User Interface),圖形用戶界面,分爲:AWT,Swing
1)AWT(Abstract Window ToolKit),抽象窗戶工具集,第一代的Java GUI組件,是重量級的。
三、練習
1)通過命令行輸入一串字符,首先判斷這些字符是否都爲數字。如果其中有字符不是數字,那麼直接在命令行山輸出“用戶輸入的字符不都爲數字,無法轉換”,程序結束;如果用戶輸入的字符都爲字符,那麼將該數字轉換爲中文的大寫表示(用戶每次最多輸入八個字符);
輸入輸出例子:
輸入:1234567
輸出:一百二十三萬四千五百六十七
輸入:0001
輸出:一
輸入:1001
輸出:一千零一
輸入:1100
輸出:一千一百
輸入:10100
輸出:一萬零一百
輸入:0010
輸出:十
輸入:000000001
輸出:一
package digitchange;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class DigitChange
{
private static final HashMap<String, String> map = new HashMap<String, String>(); //key存儲數字,value存儲數字對應的漢字
static
{
map.put("0", "零");
map.put("1", "壹");
map.put("2", "貳");
map.put("3", "叄");
map.put("4", "肆");
map.put("5", "伍");
map.put("6", "陸");
map.put("7", "柒");
map.put("8", "捌");
map.put("9", "玖");
map.put("10", "拾");
}
private void check(String str) //用於檢測用戶輸入的字符串是否合法
{
for(int i = 0; i < str.length(); i++)
{
if(str.charAt(i) >= '0' && str.charAt(i) <= '9')
{
}
else
{
System.out.println("用戶輸入的字符不都爲數字,無法轉換");
System.exit(0);
}
}
}
private void lengthIsOne(String str) //輸入字符個數爲1的轉換方法
{
System.out.print(map.get(str));
}
private void lengthIsTwo(String str) //輸入字符個數爲2的轉換方法
{
if(str.charAt(0) == '0')
{
this.lengthIsOne(str.substring(1, 2));
}
else if(str.charAt(1) == '0')
{
if("10".equals(str))
{
System.out.print("拾");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "拾");
}
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "拾" + map.get(str.substring(1, 2)));
}
}
private void lengthIsThree(String str) //輸入字符個數爲3的轉換方法
{
if(str.charAt(0) == '0')
{
this.lengthIsTwo(str.substring(1, 3));
}
else if(str.charAt(1) == '0')
{
if("00".equals(str.substring(1,3)))
{
System.out.print(map.get(str.substring(0, 1)) + "百");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "百零" + map.get(str.substring(2, 3)));
}
}
else if(str.charAt(2) == '0')
{
System.out.print(map.get(str.substring(0, 1)) + "百" + map.get(str.substring(1, 2)) + "拾");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "百" + map.get(str.substring(1, 2)) + "拾"
+ map.get(str.substring(2, 3)));
}
}
private void lengthIsFour(String str) //輸入字符個數爲4的轉換方法
{
if(str.charAt(0) == '0')
{
this.lengthIsThree(str.substring(1, 4));
}
else if(str.charAt(1) == '0')
{
if("000".equals(str.substring(1, 4)))
{
System.out.print(map.get(str.substring(0, 1)) + "仟");
}
else if("10".equals(str.substring(2, 4)))
{
System.out.print(map.get(str.substring(0, 1)) + "仟零拾");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "仟零");
this.lengthIsTwo(str.substring(2, 4));
}
}
else if(str.charAt(2) == '0')
{
if("00".equals(str.substring(2, 4)))
{
System.out.print(map.get(str.substring(0, 1)) + "仟" + map.get(str.substring(1, 2)) + "百");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "仟" + map.get(str.substring(1, 2)) + "百零");
this.lengthIsOne(str.substring(3, 4));
}
}
else if(str.charAt(3) == '0')
{
System.out.print(map.get(str.substring(0, 1)) + "仟 " + map.get(str.substring(1, 2)) + "百" + map.get(str.substring(2, 3))
+ "拾");
}
else
{
System.out.print(map.get(str.substring(0, 1)) + "仟 " + map.get(str.substring(1, 2)) + "百" + map.get(str.substring(2, 3))
+ "拾" + map.get(str.substring(3, 4)));
}
}
private void lengthIsFive(String str) //輸入字符個數爲5的轉換方法
{
String s1 = str.substring(0, 1);
String s2 = str.substring(1);
if(s1.charAt(0) == '0')
{
this.lengthIsFour(s2);
}
else if(s2.charAt(0) == '0')
{
if("0000".equals(s2))
{
this.lengthIsOne(s1);
System.out.print("萬");
}
else
{
this.lengthIsOne(s1);
System.out.print("萬零");
this.lengthIsThree(s2.substring(1));
}
}
else
{
this.lengthIsOne(s1);
System.out.println("萬");
this.lengthIsFour(str.substring(1));
}
}
private void lengthIsSix(String str) //輸入字符個數爲6的轉換方法
{
String s1 = str.substring(0, 2);
String s2 = str.substring(2);
if("00".equals(s1))
{
this.lengthIsFour(s2);
}
else if(s2.charAt(0) == '0')
{
if("0000".equals(s2))
{
this.lengthIsTwo(s1);
System.out.print("萬");
}
else
{
this.lengthIsTwo(s1);
System.out.print("萬零");
this.lengthIsThree(s2.substring(1));
}
}
else
{
this.lengthIsTwo(s1);
System.out.print("萬");
this.lengthIsFour(s2);
}
}
private void lengthIsSeven(String str) //輸入字符個數爲7的轉換方法
{
String s1 = str.substring(0, 3);
String s2 = str.substring(3);
if("000".equals(s1))
{
this.lengthIsFour(s2);
}
else if(s2.charAt(0) == '0')
{
if("0000".equals(s2))
{
this.lengthIsThree(s1);
System.out.print("萬");
}
else
{
this.lengthIsThree(s1);
System.out.print("萬零");
this.lengthIsThree(s2.substring(1));
}
}
else
{
this.lengthIsThree(s1);
System.out.print("萬");
this.lengthIsFour(s2);
}
}
private void lengthIsEight(String str) //輸入字符個數爲八的轉換方法
{
String s1 = str.substring(0, 4);
String s2 = str.substring(4);
if("0000".equals(s1))
{
this.lengthIsFour(s2);
}
else if(s2.charAt(0) == '0')
{
if("0000".equals(s2))
{
this.lengthIsFour(s1);
System.out.print("萬");
}
else
{
this.lengthIsFour(s1);
System.out.print("萬零");
this.lengthIsThree(s2.substring(1));
}
}
else
{
this.lengthIsFour(s1);
System.out.print("萬");
this.lengthIsFour(s2);
}
}
public void change(String str) //對用戶輸入的字符進行檢查和轉換
{
this.check(str);
if(str.length() == 1)
{
this.lengthIsOne(str);
}
else if(str.length() == 2)
{
this.lengthIsTwo(str);
}
else if(str.length() == 3)
{
this.lengthIsThree(str);
}
else if(str.length() == 4)
{
this.lengthIsFour(str);
}
else if(str.length() == 5)
{
this.lengthIsFive(str);
}
else if(str.length() == 6)
{
this.lengthIsSix(str);
}
else if(str.length() == 7)
{
this.lengthIsSeven(str);
}
else
{
this.lengthIsEight(str);
}
System.out.println();
}
public static void main(String[] args) throws IOException
{
System.out.println("請輸入字符(字符個數最多爲八個):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
str = br.readLine();
while(str.length() == 0)
{
System.out.println("用戶未輸入字符,請輸入字符(字符個數最多爲八個):");
str = br.readLine();
}
while(str.length() > 8)
{
System.out.println("輸入字符個數超過八個,請重新輸入:");
str = br.readLine();
}
DigitChange d = new DigitChange();
d.change(str);
}
}
2)
編寫一個小型的應用程序框架,該框架會向用戶公開一個run方法,方法聲明爲:
public void run(String className);
該方法位於類ApplicationRun類中,該類的聲明爲:
public class ApplicationRun
{
public void run(String className)
{
//這裏是方法的執行代碼
}
}
其中的字符串參數className爲用戶提供一個類的全名(包名+類名),當用戶將類的全名一字符串的形式傳遞給該run方法時,run方法會自動執行用戶所提供的類中所有被@Test註解所修飾的public void 且不帶參數的方法。
@Test註解爲該小型應用程序框架所定義的,用戶可以使用該註解修飾自己的方法,同時該@Test註解只能用於修飾方法。
package applicationrun;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface Test
{
String value() default "hello";
}
package applicationrun;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ApplicationRun
{
private Class<?> classType;
public void run(String className) throws ReflectiveOperationException
{
classType = Class.forName(className);
Object obj = classType.newInstance();
Method[] method = classType.getDeclaredMethods();
for(Method m : method)
{
if(void.class.equals(m.getReturnType()) && m.getModifiers() == Modifier.PUBLIC
&& m.getParameterTypes().length == 0)
{
if(m.isAnnotationPresent(Test.class))
{
m.invoke(obj, new Object[]{});
}
}
}
}
}