1.利用白富美接口案例,土豪征婚使用匿名内部类对象实现。
interface IWhite
{
abstract public void skinWhite();
}
interface IRich
{
abstract public void rich();
}
interface IBeanti
{
abstract public void beanti();
}
interface IWRB extends IWhite,IRich,IBeanti
{
}
class EarchRicher
{
public void marring(IWRB wbr)
{
wbr.skinWhite();
wbr.rich();
wbr.beanti();
System.out.println("marring");
}
}
class InterfaceDemo2
{
public static void main(String [] args)
{
EarchRicher richMan = new EarchRicher();
//匿名内部类实现接口的所有抽象方法
richMan.marring(new IWRB(){
public void skinWhite()
{
System.out.println("白");
}
public void rich()
{
System.out.println("富");
}
public void beanti()
{
System.out.println("美");
}
});
}
}
2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度,
构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。
class TrianleTooSmallException extends Exception
{
private String info;
public TrianleTooSmallException()
{
}
public TrianleTooSmallException(String info)
{
this.info = info;
}
public String getInfo()
{
return info;
}
public void setInfo(String info)
{
this.info = info ;
}
}
class TrianleNotMatchException extends Exception
{
private String info;
public TrianleNotMatchException()
{
}
public TrianleNotMatchException(String info)
{
this.info = info ;
}
public String getInfo()
{
return info;
}
public void setInfo()
{
this.info = info ;
}
}
class Trianle
{
private int a;
private int b;
private int c;
public int getA()
{
return a;
}
public void setA(int a) throws TrianleTooSmallException
{
if (a > 0)
{
this.a = a;
}else
{
throw new TrianleTooSmallException("边长不能小于0");
}
}
public int getB()
{
return b;
}
public void setB(int b) throws TrianleTooSmallException
{
if (b >0)
{
this.b = b ;
}
else
{
throw new TrianleTooSmallException("边长不能小于0");
}
}
public int getC()
{
return c;
}
public void setC(int c) throws TrianleTooSmallException
{
if (c > 0)
{
this.c = c;
}
else
{
throw new TrianleTooSmallException("边长不能小于0");
}
}
public static void Checking(int a , int b , int c) throws TrianleNotMatchException
{
if (a + b <= c || a + c <= b || b + c <= a)
{
throw new TrianleNotMatchException("两边之和必须大于第三边");
}
else
{
System.out.println("是三角形");
}
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Trianle trianle = new Trianle();
try
{
trianle.setA(4);
trianle.setB(3);
trianle.setC(5);
trianle.Checking(trianle.getA(),trianle.getB(),trianle.getC());
}
catch (TrianleTooSmallException t)
{
System.out.println(t.getInfo());
System.exit(-1);
}
catch (TrianleNotMatchException t)
{
System.out.println(t.getInfo());
System.exit(-1);
}
catch (Exception e)
{
System.out.println("不是一个三角形");
System.exit(-1);
}
}
}
3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,
要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。
class Person
{
private String birthday;
public String getBirthday()
{
return birthday;
}
public void setBirthday(int y , int m , int d) throws InvaidException
{
if (y < 1970 || y > 2016)
{
throw new InvaidException("年代太远,请输入1970 ~~ 2016 之间的年份");
}
if (m <=0 || m > 12)
{
throw new InvaidException("非法月份");
}
else
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (d <= 0 || d > 31)
{
throw new InvaidException("非法日期");
}
break;
case 4:
case 6:
case 9:
case 11:
if (d <= 0 || d > 30)
{
throw new InvaidException("非法日期");
}
break;
case 2:
if (y % 4 != 0 && d == 29 | d > 29)
{
throw new InvaidException("非法日期");
}
break;
}
}
this.birthday = y + "/" + m + "/" + d;
}
}
class InvaidException extends Exception
{
public InvaidException(String msg)
{
super(msg);
System.out.println(msg);
}
}
class BirthdayDemo
{
public static void main(String[] args)
{
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
try
{
p1.setBirthday(1999,12,3);
p2.setBirthday(2009,1,22);
p3.setBirthday(2011,12,9);
System.out.println(p1.getBirthday());
System.out.println(p2.getBirthday());
System.out.println(p3.getBirthday());
}
catch (InvaidException e)
{
}
}
}
4.将类定义到指定的包下。com.it18zhang,编译之后,打成jar文件。
jar cvf myjar.jar -C classes/ .
jar cvfe myjar.jar com.it18zhang.A -C classes/ .
java -jar myjar.jar
java -jar myjar.jar com.it18zhang.A
转载于:https://blog.51cto.com/45279/1749622