java课程第七天,匿名内部类以及异常处理

  1. 利用白富美接口案例,土豪征婚使用匿名内部类对象实现。

//定义女人皮肤白

public interface Iwhite {

public abstract void white();

}

//定义女人有钱

public interface Irich {

public abstract void rich();

}

//定义女人漂亮

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) {


}

}

}


你可能感兴趣的:(内部类,匿名,异常类)