利用白富美接口案例,土豪征婚使用匿名内部类对象实现。
2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度,
构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。
3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,
要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。
4.将类定义到指定的包下。com.it18zhang,编译之后,打成jar文件。
5.相互之间使用jar包,放置cp下,对class进行重用。
6.设计程序,考查修饰符。public -> protected -> default -> private(选做题)
7.预习多线程。
--------------------------------------------------------------------------------------------
class Demo
{
public static void main(String[] args)
{
RicherMan man=new RicherMan();
man.marry(new IAll(){
public void say(){
System.out.println("我是白富美")
}
})
System.out.println("Hello World!");
}
}
interface IWhite
{
}
interface IRich
{
}
interface IBeauty
{
}
interface IAll extends Iwhite,IRich,IBeauty
{
public void Say();
}
class RicherMan
{
public void marry(IAll woman){
System.out.println("迎娶白富美");
}
class Birthday
{
int year;
int month;
int day;
public boolean IsFullYear(int year){
//实现一个判断闰年或者平年的算法
return true;
}
public Birthday(int year,int month,int day){
if(year<0||month<0||day<0){
throw new Exception("日期不存在负数");
}
if(month>12){
throw new Exception("月份应小于12");
}
int max;
switch(month){
case 2:if(day>(max=IsFullYear(year)?29:28))
throw new Exception("当年的2月份不应超过"+max+"天");
break;
case 4:
case 6:
case 9:
case 11:if(day>30) throw new Exception("4,6,9,11月份的天数不应超过30天");
break;
default:if(day>31) throw new Exception("1,3,5,7,8,10,12月份的天数不应超过31天");
}
this.year=year;
this.month=month;
this.day=day;
}
}
class Person
{
private Birthday birthday;
Person(Birthday birthday){
this.birthday=birthday;
}
}
class TriangleFormedFailure extends Exception
{
}
class Triangle
{
private int thread1;
private int thread2;
private int thread3;
public Triangle(int thread1,int thread2,int thread3){
if((thread1+thread2)<thread3||(thread1+thread3)<thread2||(thread2+thread3)<thread1){
throw new TriangleFormedFailure("两边之和小于第三边无法形成三角形");
}else{
this.thread1=thread1;
this.thread2=thread2;
this.thread3=thread3;
}
}
}
IT十八掌作业_java基础第七天_匿名内部类、异常、包和jar
原文:http://11102728.blog.51cto.com/11092728/1748796