黑马程序员技术交流社区

标题: 异常处理的 [打印本页]

作者: 蒋磊磊    时间: 2015-3-17 09:23
标题: 异常处理的
建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。

class NoValueException extends RuntimeException
{
        NoValueException(String message)
        {
                super(message);
        }
}
interface Shape
{
        void getArea();
}
class Rec implements Shape
{
        private int len,wid;
        Rec(int len ,int wid)
        {
                if(len<=0 || wid<=0)
                        throw new NoValueException("出现非法值");
                this.len = len;
                this.wid = wid;
        }
        public void getArea()
        {
                System.out.println(len*wid);
        }
}
class Circle implements Shape
{
        private int radius;
        public static final double PI = 3.14;
        Circle(int radius)
        {
                if(radius<=0)
                        throw new NoValueException("非法");
                this.radius = radius;
        }
        public void getArea()
        {
                System.out.println(radius*radius*PI);
        }
}
class  ExceptionTest1
{
        public static void main(String[] args)
        {               
                Rec r = new Rec(3,4);
                r.getArea();
                Circle c = new Circle(-8);
                System.out.println("over");
        }
}
问题:
class NoValueException extends RuntimeException
{
        NoValueException(String message)
        {
                super(message);
        }
}
这个类是执行后是 super(message)即RuntimeException(message)这么就会有输出"出现非法值"呢?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2