您的位置:首页 > 编程语言 > Java开发

java基础第七天

2016-05-18 16:08 567 查看
1、利用白富美接口案例,土豪征婚使用匿名内部类对象实现

package com.cn;

public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub

RichMan r = new RichMan();
r.marry(new IWhite(){

public void white() {
System.out.println("haha");

}

});
}

}

interface IWhite {
public abstract void white();
}

interface IRich {
public void rich();
}

interface IBeauti {
public void beauti();
}

interface IWRB extends IWhite, IRich, IBeauti {
}

class RichMan {
public void marry(IWhite i) {
System.out.println("白好");
i.white();
}

public void marry(IRich i) {
System.out.println("富很好");
i.rich();
}

public void marry(IBeauti i) {
System.out.println("美非常好");
i.beauti();
}

public void marry(IWRB i) {
System.out.println("白富美最好不过");
i.white();
i.rich();
i.beauti();
}
}

2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度, 构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。package com.cn;

public class Demo {

public static void main(String[] args) throws Exception {
Trianle t = new Trianle(1, 2, 3);
t.chech();
}

}

class myExcepton extends Exception {
String info;
public myExcepton(String info){
this.info = info;

}
}

class Trianle {

private int a;
private int b;
private int c;

public Trianle(int a,int b, int c){
this.a = a;
this.b = b;
this.c = c;
}

public void chech(int a,int b, int c) {
if (a + b < c || a + c < b || b + c < a) {
new myExcepton("bo bo ");
}

}

public void chech() throws Exception {
if (a + b < c || a + c < b || b + c < a) {
throw new myExcepton("bo bo ");
}
System.out.println("hao hao ");
}
}
3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理, 要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。答:
public class Brithday {private String birthday;
public String getBirthday() {return birthday;}
public void setBirthday(int year, int month, int date)throws InvalidParamExcetion {
if (year < 1900 || year > 2016) {throw new InvalidParamExcetion("年份不合适,请传入1900年到2016年之间的年份");}
if (month <= 0 || month > 12) {throw new InvalidParamExcetion("月份不合适,不存在" + month + "月");}
if (date > 31 || date <= 0) {throw new InvalidParamExcetion("日期不合适,不存在" + date + "日");}boolean isThirdOne = date == 1 || date == 3 || date == 5 || date == 7|| date == 8 || date == 10 || date == 12;if (!isThirdOne && date == 31) {throw new InvalidParamExcetion("日期不合适," + month + "月不存在" + date+ "日");}if (month == 2 && date > 29) {throw new InvalidParamExcetion("日期不合适,2月不存在" + date + "日");}if (year % 4 != 0 && month == 2 && date == 29) {throw new InvalidParamExcetion("日期不合适," + year + "不是闰年,所以2月不存在"+ date + "日");}
System.out.println("生日为:" + year + "年" + month + "月 " + date + "日 ");}
public static void main(String[] args) throws InvalidParamExcetion {Brithday person = new Brithday();try {person.setBirthday(2015, 12, 5);person.setBirthday(2016, 2, 29);person.setBirthday(2015, 2, 29);
person.setBirthday(2015, 3, 5);} catch (InvalidParamExcetion e) {}
}
}

class InvalidParamExcetion extends Exception {
public InvalidParamExcetion(String msg) {super(msg);System.out.println(msg);}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java基础