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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 孙百鑫 于 2013-6-17 20:49 编辑

这有两段代码 ,都可以实现,运行结果都为12   over,你觉得哪段代

  1. <P>/*
  2. 有一个圆形和长方形。
  3. 都 可以获取面积。对于面积如果出现非法的数值,视为是获取面积出现问题。
  4. 问题通过异常来表示。
  5. 先要对这个程序进行基本设计。
  6. */</P>
  7. <P>interface Shape
  8. {
  9.        public void getArea(int length, int width);
  10. }
  11. /*
  12. 随便试问如果改成以下类的方式,乐不乐观?如果不乐观,那么它又有什么局限性?</P>
  13. <P>class Shape
  14. {
  15.        public void getArea(int length, int width)
  16.        {</P>
  17. <P>       }
  18. }
  19. */
  20. class Rec implements/*宝义成class类后改成extends*/ Shape
  21. {
  22.        private int length,width;
  23.        public void getArea(int length, int width)
  24.       {
  25.               this.length = length;
  26.               this.width = width;
  27.               System.out.println(length*width);
  28.       }
  29. }

  30. class ExceptionTest2
  31. {
  32.         public static void main(String[] args)
  33.        {
  34.               Rec a = new Rec();
  35.               a.getArea(3,4);

  36.               System.out.println("over");
  37.         }
  38. }

  39. /****************************************************************/
  40. interface Shape
  41. {
  42.        public void getArea();
  43. }

  44. class Rec implements Shape
  45. {
  46.        private int length,width;
  47.        Rec(int length, int width)
  48.       {
  49.               this.length = length;
  50.               this.width = width;
  51.       }
  52.        public void getArea()
  53.       {
  54.               System.out.println(length*width);
  55.       }
  56. }

  57. class ExceptionTest3
  58. {
  59.        public static void main(String[] args)
  60.       {
  61.               Rec a = new Rec(3,4);
  62.               a.getArea();

  63.               System.out.println("over");
  64.       }
  65. }
  66. </P>
复制代码
码会更好些,为什么?那么另一段代码有什么局限性(比如内存分配等)?希望大神们能详细回答》

评分

参与人数 1技术分 +1 收起 理由
曹睿翔 + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览
貌似有点乱码?但应该不影响大伙阅读代码,问题是第一行跳到最后一行了,失误了。。
回复 使用道具 举报
slatop@qq.com 发表于 2013-6-16 02:22
先回答你的第一个问题:
将接口改成类后在你的程序中没什么本质区别,但是如果换做实现开发中应该以接口定 ...

太感谢哥们你了,谢谢,解释得非常详细,解决我不少的疑问。。。。支持黑马!学员很给力啊!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马