我的练习题

来源:10-3 Java 中的抽象类

maillengxue

2015-07-17 13:15

 

package com.imooc;

public abstract class Shape {
 double a;
 double b;
 double r;
 public abstract void l();
 public abstract void s();

}


package com.imooc;

public class Rectangle extends Shape {

 @Override
 public void l() {
  // TODO Auto-generated method stub
  Object l1 = 2*(a+b);
  System.out.println(l1);
 }

 @Override
 public void s() {
  // TODO Auto-generated method stub
  double s = a*b;
  System.out.println(s);
 }

}


package com.imooc;

public class Circle extends Shape {

 final double PI = 3.14;

 @Override
 public void l() {
   // TODO Auto-generated method stub
  Object l = 2*PI *r;
  System.out.println(l);
 }

 @Override
 public void s() {
  // TODO Auto-generated method stub
  double s = PI*r*r;
  System.out.println(s);
 }

}


package com.imooc;

public class Test {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Shape shape = new Rectangle();
  shape.a=6.0;
  shape.b=4.0;
  shape.l();
  shape.s();
  
  Shape shape2 = new Circle();
  shape2.r = 4.0;
  shape2.l();
  shape2.s();
  

 }

}

 

写回答 关注

1回答

  • 飞扬拽拽
    2015-12-18 09:47:35

    Rectangle just have a and b, and donot have r.

    Circle just have r, and donot have a and b.

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530654 学习 · 6091 问题

查看课程

相似问题