- /*
- 有一个圆形和长方形。都可以获取面积。对于面积如果出现非法的数值,视为是获取面积出现的问题
- 问题通过异常来表示
- */
- interface Shape
- {
- void getArea();
- }
- class NoValueException extends RuntimeException
- {
- NoValueException(String message)
- {
- super(message);
- }
- }
- class Circle implements Shape
- {
- private int radius;
- public static final double NUM=3.14;
- Circle(int radius)
- {
- if(radius<0)
- throw new NoValueException("半径不合法");
- this.radius=radius;
- }
- public void getArea()
- {
- System.out.println(radius*radius*NUM);
- }
- }
- class Rec implements Shape
- {
- private int len,wid;
- Rec(int len,int wid) //throws NoValueException
- {
- if( len<=0||wid<=0)
- throw new NoValueException("数值出现异常");
- else
- {
- this.len=len;
- this.wid=wid;
- }
- }
- public void getArea()
- {
- System.out.println(len*wid);
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- //Rec r=new Rec(3,-4);
- //r.getArea();
- //System.out.println("over");
- Circle c=new Circle(-1);
- c.getArea();
- }
- }
复制代码
|
|