*/
//定义一个计算面积的函数
interface Shape {
public abstract void getArea();
}
class NoValueException extends RuntimeException {
NoValueException(String mas) {
super(mas);
}
}
//定义一个圆类
class Crecle implements Shape {
public static final double PI = 3.14; //定义一个常量
private int ban; //定义半径
Crecle(int ban) {
if(ban <= 0)
throw new NoValueException("非法数据类型");
this.ban = ban;
}
//定义一个函数计算圆的面积
public void getArea() {
System.out.println(ban * ban * PI);
}
}
//定义一个长方形的类
class Rec implements Shape {
private int ch, ku; //定义长方形的长和宽
Rec(int ch, int ku) {
if(ch <= 0 || ku <= 0)
throw new NoValueException("非法操作");
this.ch = ch;
this.ku = ku;
}
//覆盖接口的函数。求面积
public void getArea() {
System.out.println(ch * ku);
}
}
//运行类
class ExceptionTest {
public static void main(String[] args) {
Crecle c = new Crecle(3);
c.getArea();
Rec r = new Rec(3, 2);
r.getArea();
}
}