Java 抽象类,未重写方法的子类

Java 抽象类,未重写方法的子类,java,Java,在发布这个问题之前我搜索了很多,但我得到的只是更多的问题需要回答。我是java新手,所以我的问题可能很简单 无论如何,我的问题是: 那是我的超级班 public abstract class Shape { protected String color; // Constructor public Shape (String color) { this.color = color; } public String toStr

在发布这个问题之前我搜索了很多,但我得到的只是更多的问题需要回答。我是java新手,所以我的问题可能很简单 无论如何,我的问题是: 那是我的超级班

    public abstract class Shape 
{

   protected String color;


   // Constructor
   public Shape (String color) 
   {
        this.color = color;
   }

   public String toString() 
   {
        return "Color = " + color;
   }


   abstract double getArea();
}
那是我的孩子课:

    public class Rectangle extends Shape 
{
    private int width;
    private int length;


   // Constructor
   public Rectangle(String color, int length, int width) 
   {
      super(color);
      this.length = length;
      this.width = width;
   }


   public String toString() 
   {
      return  "color = " + color + "\nlength = " + this.length + "\nwidth = " + this.width;
   }
   public double getArea() 
   {
        return length*width;
   }


}
以及主要代码:

    public class TestShape 
{
   public static void main(String[] args) 
   {
        Shape a = new Rectangle("RED",10,5);
        a.getArea();

   }
}
我的问题是,尽管它编译得很好,但我没有得到任何结果,我想知道为什么子类中的方法没有重写抽象类的方法。感谢您的帮助

我的问题是,尽管它编译得很好,但我没有得到任何结果

您需要添加一个print语句来将方法调用的结果打印回控制台,因此System.out.printlna.getArea

我想知道为什么我在子类中的方法不重写抽象类的方法

它与对象类中的toString方法一样。如果没有,编译器将抛出一个错误,说明并非抽象类中的所有抽象方法都已实现。如果您正在实现接口,也会出现这种情况


但是,最好在方法上方添加@Override注释,以表示该方法已被重写。Eclipse或Netbeans之类的IDE应该自动为您添加此功能。

如果您在通话中使用system.out.print来获取区域,您可能会看到输出,但您可能会返回该区域,但您不使用它,也就是说,您点式打印它。请执行system.out.printlna.getArea;你得到了什么?是的,成功了,非常感谢。就像我说的我是个笨蛋!System.out.printlna.getArea;给你50.0分