0

Hi I am trying to execute this code in Eclipse.

abstract class ShapeNew {
    int length;
    public abstract double area();
}
 class Rect extends Shape{
    Rect(int side){
        this.length = side;
    }
    public double area(){
        System.out.println("area of rectangle"+ length*length);
        return length*length;
    }



    /**
     * @param args
     */
public static class Area{
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ShapeNew rect = new Rect(32);// I am unable to use this. The eclipse throws an error//

        rect.area();
    }

}

Can anyone help me with it. Why am I unable to assign a reference of ShapeNew to an object of Rect.? I get this error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from Rect to ShapeNew
4

5 回答 5

6

你有一个错字:你需要从你定义的类扩展:

class Rect extends ShapeNew {

(此外,给定面积函数的正方形长度,您似乎已经实现了正方形而不是矩形)。

于 2013-07-08T07:46:50.383 回答
2

您必须替换此行:

class Rect extends Shape

经过

class Rect extends ShapeNew
于 2013-07-08T07:56:06.247 回答
2

我认为这是一个简单的错字。您的抽象类名称是第 1 行的 ShapeNew,您正在尝试获取 Shape 类的新实例。将形状更改为 ShapeNew

于 2013-07-08T07:51:11.910 回答
0

您收到一个错误,因为您抽象类是 :ShapeNew并且您从错误的名称继承子类:Shape

因此,只需将此名称更改为 ShapeNew。

 class Rect extends ShapeNew{
于 2013-07-08T08:17:36.307 回答
0
  abstract class ShapeNew {
  int length;
  public abstract double area();
                          }
  class Rect extends ShapeNew{
  Rect(int side){
    this.length = side;
}
public double area(){
    System.out.println("area of rectangle"+ length*length);
    return length*length;
}
 }


public  class Area{
public static void main(String[] args) {
    // TODO Auto-generated method stub

    ShapeNew rect = new Rect(32);

    rect.area();
}

 }
于 2013-07-08T08:00:50.627 回答