java課程第七天,匿名內部類以及異常處理

  1. 利用白富美接口案例,土豪徵婚使用匿名內部類對象實現。ide

//定義女人皮膚白this

public interface Iwhite {spa

public abstract void white();orm

}對象

//定義女人有錢接口

public interface Irich {ci

public abstract void rich();get

}it

//定義女人漂亮io

public interface Ibeauti {

public abstract void beauti();

}

//定義白富美

public interface IWRB extends Iwhite, Irich, Ibeauti {


}

//定義有錢人娶老婆的標準是白富美

public class richer {

public void marry(IWRB i) {

i.white();

i.rich();

i.beauti();

}

}


public class richerDemo {

public static void main(String[] args) {

richer rm = new richer();

rm.marry(new IWRB() {                        //實現匿名內部類

public void white() {

System.out.println("我很白,來草我!");

}


public void rich() {

System.out.println("我頗有錢,來草我!");

}


public void beauti() {

System.out.println("我很漂亮,來草我!");

}

});

}

}


2.定義三角形類Trianle,裏面包含三個int類型屬性,分別表示三條邊的長度, 造三角形對象時,任意兩邊之和是否大於第三邊,如若不成立,拋出自定義異常。

class EdgeTooSmallException extends Exception { // 這個異常是用來判斷邊小於或等於0的

private String info;


public EdgeTooSmallException() {

}


public EdgeTooSmallException(String info) {

this.info = info;

}


public String getInfo() {

return info;

}


public void setEdgeInfo(String info) {

this.info = info;

}

}


class EdgeNotMatchException extends Exception { // 這個異常是用來判斷三角形的

private String info;


public EdgeNotMatchException() {


}


public EdgeNotMatchException(String info) {

this.info = info;

}


public String getInfo() {

return info;

}


public void setEdgeNotMatchException(String info) {

this.info = info;

}

}


class Triangle {

// 三條邊

private int edgea;

private int edgeb;

private int edgec;


// edgea的get/set方法

public int getEdgea() {

return edgea;

}


public void setEdgea(int edgea) throws EdgeTooSmallException {

if (edgea > 0) {

this.edgea = edgea;

} else {

throw new EdgeTooSmallException("邊長A不能小於或等於0");

}

}


// edgeb的get/set方法

public int getEdgeb() {

return edgeb;

}


public void setEdgeb(int edgeb) throws EdgeTooSmallException {

if (edgeb > 0) {

this.edgeb = edgeb;

} else {

throw new EdgeTooSmallException("邊長B不能小於或等於0");

}

}


// edgec的get/set方法

public int getEdgec() {

return edgec;

}


public void setEdgec(int edgec) throws EdgeTooSmallException {

if (edgec > 0) {

this.edgec = edgec;

} else {

throw new EdgeTooSmallException("邊長C不能小於或等於0");

}

}


// 判斷是否是三角形

public static void Checking(int x, int y, int z) throws EdgeNotMatchException {

if (x + y <= z || x + z <= y || y + z <= x) {


throw new EdgeNotMatchException("這不是三角形");

} else {

System.out.println("這是個三角形");

}

}

}


class TriangleDemo {

public static void main(String[] args) {

// 構造三角形

Triangle t1 = new Triangle();


// 設定三條邊,找出異常

try {

t1.setEdgea(4);

t1.setEdgeb(5);

t1.setEdgec(3);

t1.Checking(t1.getEdgea(), t1.getEdgeb(), t1.getEdgec());

} catch (EdgeTooSmallException e) {

System.out.println(e.getInfo());

System.exit(-1);

} catch (EdgeNotMatchException e) {

System.out.println(e.getInfo());

System.exit(-1);

}


System.out.println("邊長爲" + t1.getEdgea() + "," + t1.getEdgeb() + "," + t1.getEdgec());


}

}


3.Person類中增長birthday屬性,對setBirthday(int ,int , int )方法進行異常處理,要求年有效、月有效、日有效、年月日指定的具體日期有效,對不一樣狀況分別拋出不一樣的異常。

public class invalidParamException extends Exception {

  public invalidParamException(String msg){

 //super(msg);

 System.out.println("你麻痹");

 System.out.println(msg);

  }

}

public class Birthday {

private String birthday;


public String getBirthday() {

return birthday;

}


public void setBirthday(int year, int month, int date) throws invalidParamException {

// 判斷年份

if (year < 1900 || year > 2016) {

throw new invalidParamException("年份不合適,請輸入1900年到2016年的年份");

}

if (month < 0 || month > 12) {

throw new invalidParamException("月份不合適,不存在" + month + "月");

}

if (date < 0 || date > 31) {

throw new invalidParamException("日期不合適,不存在" + date + "日");

}

boolean isThirdOne = date == 1 || date == 3 || date == 5 || date == 7 || date == 8 || date == 10 || date == 12;

if (!isThirdOne && date == 31) {

throw new invalidParamException("日期不合適," + month + "月不存在" + date + "日");

}

if (month == 2 && date > 29) {

throw new invalidParamException("日期不合適,二月不存在" + date + "日");

}

if (year % 4 != 0 && month == 2 && date == 29) {

throw new invalidParamException("日期不合適," + year + "不是閏年,因此2月不存在" + date + "日");

}

System.out.println("生日爲:" + year + "年" + month + "月  " + date + "日 ");

}

public class birthdayDemo {

public static void main(String[] args) throws invalidParamException {

Birthday person = new Birthday();

try {

person.setBirthday(2015, 12, 5);

person.setBirthday(2016, 2, 29);

person.setBirthday(2015, 2, 29);

person.setBirthday(2015, 3, 5);

} catch (invalidParamException e) {


}

}

}

相關文章
相關標籤/搜索