日期:2014-05-20  浏览次数:20810 次

java 最简单的多态运用,还是老出错
PassTest3.java:2: 错误: 缺少方法主体, 或声明抽象
  public int getPerimeter();
  ^
PassTest3.java:3: 错误: 缺少方法主体, 或声明抽象
  public int getArea();
  ^
2 个错误

这是错误提示



abstract class GeometricObject{
  public int getPerimeter();
  public int getArea();
}
 

class Rectangle extends GeometricObject{
  private int x1;
  private int x2;
  private int y1;
  private int y2;

  public Rectangle(int x1,int x2,int y1,int y2){
  this.x1=x1;
  this.x2=x2;
  this.y1=y1;
  this.y2=y2;
  }
  public int getHeight(){
  int aHeight=0;
  aHeight=Math.abs(x1-x2);
  return aHeight;
  }
  public int getWidth(){
  int aWidth=0;
  aWidth=Math.abs(y1-y2);
  return aWidth;
  }
  public int getPerimeter(){
  return getHeight()*2+getWidth()*2;
  }
  public int getArea(){
  return getHeight()*getWidth();
  }
}

public class PassTest3{
  public static void main(String[] args){
  GeometricObject g=new Rectangle(0,5,1,5);
  System.out.println(g.getArea() + "----" + g.getPerimeter());
  }
}



PassTest3.java:2: 错误: 缺少方法主体, 或声明抽象
  public int getPerimeter();
  ^
PassTest3.java:3: 错误: 缺少方法主体, 或声明抽象
  public int getArea();
  ^
2 个错误

------解决方案--------------------
abstract class GeometricObject{
public abstract int getPerimeter();
public abstract int getArea();
}
方法写成抽象的