A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 蒋磊磊 中级黑马   /  2015-3-17 09:23  /  862 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

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)这么就会有输出"出现非法值"呢?
您需要登录后才可以回帖 登录 | 加入黑马