以前学的都是C++,刚开始接触java,做这project花了一星期,无果,只能上来求解,回去自己找错了
本程序目的是学习并掌握类的继承,静态联编和动态联编(掌握多态方法或函数的设计)。
二 实验内容和步骤: 对给定的下列几何图形抽象类
abstract class Shape {
   private double xPos;
   private double yPos;
   
   public Shape () { xPos = 0;   yPos = 0;  }
   public Shape (double x, double y){ xPos = x;  yPos = y;  }
   abstract public double area();
   abstract public void stretchBy (double factor);  // 拉伸因子
   public final double getXPos(){ return xPos; }
   public final double getYPos(){ return yPos; }
   public void moveTo (double xLoc, double yLoc) {  xPos = xLoc;  yPos = yLoc;  }
   public String toString(){
      String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";
      return str;
   }
}
  试设计如下继承Shape类的各具体类:
  1。Triangle 类(等边三角形),Rect 类(矩形), Circle类(园)。
     注意各具体类应增置各自必须的属性域和方法,并覆盖继承下来的有关方法。
  2。ShapeTest 类。
     该类中应提供
   public void show(Shape shape){  // polymorphism method 
         System.out.println ("\n" + shape.toString());
     }
      并用如下代码显示各几何图形分别在拉伸前后的参数和面积
      System.out.println("Before stretching ...");
      for(int i = 0; i < 3; i++)
        show(shapes[i]);
      System.out.println("\n\nAfter stretched ...");
      for(int i = 0; i < 3; i++){
        shapes[i].stretchBy(2);
        show(shapes[i]);  // dynamic binding 
      }