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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© breaveheart 中级黑马   /  2013-7-24 21:06  /  1557 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

参照毕老师的视频敲的代码,但是运行出错,不知为何。附上运行错误提示。{:soso_e127:}
  1. /*
  2. 有一个圆形和长方形。
  3. 都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问题。
  4. 问题通过异常表示。
  5. 现有对这个程序进行基本设计。

  6. */

  7. class NoValueException extends RuntimeException
  8. {
  9.        NoValueException(String message);
  10. {
  11.        super(message);
  12. }
  13. }

  14.     interface Shape
  15. {
  16. void getArea();
  17. }

  18. class Rec implements Shape
  19. {
  20. private int len,wid;
  21. Rec(int len ,int wid)
  22. {
  23. if (len<=0||wid<=0)
  24. throw new NoValueException("出现非法值");
  25. this.len = len;
  26. this.wid = wid;
  27. }

  28. public void getArea()
  29. {
  30. System.out.println(len*wid);
  31. }
  32. }


  33. class ExceptionDemo
  34. {
  35. public static void main(String[] args)
  36. {


  37. Rec r = new Rec(3,4);
  38. r.getArea();
  39. System.out.println("over");
  40. }
  41. }
复制代码

11111.jpg (47.78 KB, 下载次数: 0)

11111.jpg

10 个回复

倒序浏览
本帖最后由 王磊 于 2013-7-24 21:18 编辑

class NoValueException extends RuntimeException
{
       NoValueException(String message);//这个分号去掉就好了。
       {
               super(message);
       }
}
构造方法有主体,就不能加分号,用分号的时候是表示这个类是抽象的,没有方法主体
回复 使用道具 举报
  1. /*
  2. 有一个圆形和长方形。
  3. 都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问题。
  4. 问题通过异常表示。
  5. 现有对这个程序进行基本设计。

  6. */

  7. class NoValueException extends RuntimeException
  8. {
  9.         NoValueException(String message)
  10.         {
  11.                    super(message);
  12.         }
  13. }

  14. interface Shape
  15. {
  16.         void getArea();
  17. }

  18. class Rec implements Shape
  19. {
  20.         private int len,wid;
  21.         Rec(int len ,int wid)
  22.         {
  23.                 if (len<=0||wid<=0)
  24.                         throw new NoValueException("出现非法值");
  25.                 this.len = len;
  26.                 this.wid = wid;
  27.         }

  28.         public void getArea()
  29.         {
  30.                 System.out.println(len*wid);
  31.         }
  32. }


  33. class ExceptionDemo
  34. {
  35.         public static void main(String[] args)
  36.         {


  37.                 Rec r = new Rec(3,4);
  38.                 r.getArea();
  39.                 System.out.println("over");
  40.         }
  41. }
复制代码
NoValueException类的构造函数没写对,多加了一个分号,然后就哦了
回复 使用道具 举报
第11行. 把分号去掉 试试.

NoValueException(String message)
{
       super(message);
}
回复 使用道具 举报
11行构造方法这里多了个分号 NoValueException(String message); <<--去掉就行
回复 使用道具 举报
NoValueException(String message);//把分号去掉,就可以了
{
       super(message);
}
回复 使用道具 举报
语法错误太多了
自己检查吧~
回复 使用道具 举报
NoValueException类的构造函数,多加了一个分号
回复 使用道具 举报
NoValueException(String message);//错误是这行代码不加分号
回复 使用道具 举报
昏死,谢谢大家,这种常识性问题错了没查出来。该反省反省自己了{:soso_e127:}
回复 使用道具 举报
王磊 发表于 2013-7-24 21:17
class NoValueException extends RuntimeException
{
       NoValueException(String message);//这个分号 ...

发现了,谢谢啦,这种小问题自己没看出来太不该了。谢谢:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马