Java super class toString() 和构造函数调用

Java super class toString() and constructor calling

我知道有与此类似的帖子,但其中 none 完全符合我的尝试。我是一名新程序员,刚开始阅读超级 class for Java。我创建了一个名为 super class 的形状。然后我创建了一个名为 circle 的 subclass。我在这里被两个部分难住了,从我的 subclass toString 方法调用我的 super class 中的 toString 方法,以及从我的 subclass 到我的 superclass。我在下面提供了我的 superclass、subclass 和一个主要的测试方法。

形状class:

public class Shape
{
   private boolean isFilled;
   private String color;

   public Shape()
   {
      this.isFilled = true;
      this.color = "Green";

   }


   public Shape(boolean x, String y)
   {
      this.isFilled = x;
      this.color = y;
   }   

   public void setFilled(boolean fill)
   {
      this.isFilled = fill;
   }

   public boolean getFilled()
   {
      return this.isFilled;
   }

   public void setColor(String x)
   {
      this.color = x;
   }

   public String getColor()
   {
      return this.color;
   }


   @Override
   public String toString()
   {
      String s = "Filled:" + this.isFilled + "\n";
      String t = "Color: " + this.color;
      String u = s + t;

      return u;

   }
}

圈子:

public class Circle extends Shape
{
   private double radius;


   public Circle()
   {
      this.radius = 1;
   }

   public Circle(double x)
   {
      this.radius = x;
   }


   public Circle(double radius, boolean isFilled, String Color)
   {
      super(isFilled, Color);
      this.radius = radius;

   }


   public void setRadius(double radius)
   {
      this.radius = radius;
   }


   public double setRadius()
   {
      return this.radius;
   }


   public double getArea()
   {  
      double area = this.radius * this.radius * Math.PI;

      return area;
   }


   @Override
   public String toString()
   {

      String x = "Radius: " + this.radius +"\n";
      String y = "Area: " + getArea() + "\n";

      String z = x + y;
      return z;

   }    
}

一个测试形状:

public class TestShape
{
   public static void main(String[] args)
   {

      Circle c1 = new Circle(2.67);
      System.out.println("c1: ");
      System.out.println(c1.toString());
      System.out.println();

      Circle c2 = new Circle(3, false, "Red");
      System.out.println("c2: ");
      System.out.println(c2.toString());
      System.out.println();
   }
}

预期输出:

c1:
Radius: 2.67
Area: 22.396099868176275
Filled: true
Color: Green

c2:  
Radius: 3.0
Area: 28.274333882308138
Filled: false
Color: Red

实际输出:

c1: 
Radius: 2.67
Area: 22.396099868176275

c2: 
Radius: 3.0
Area: 28.274333882308138

您的 Circle class 覆盖 parent toString 方法的行为并且 不合并 parent 结果与自己的。因此,您只能看到 Circle#toString 方法计算的内容:

@Override
public String toString() {
    String x = "Radius: " + this.radius + "\n";
    String y = "Area: " + getArea() + "\n";
    String z = x + y;
    return z;
}

即半径和面积。

如果你想添加 parents 结果,你需要使用 super.toString() 调用它并将它与你想要添加的内容结合起来,比如

@Override
public String toString() {
    // Call parent
    String parentText = super.toString();

    // Own stuff
    String x = "Radius: " + this.radius + "\n";
    String y = "Area: " + getArea() + "\n";
    String z = x + y;

    // Combine with parent
    return z + parentText;
}

您正在覆盖 toString() 方法,但您没有调用 super class 中的方法。您需要在 subclass.

中使用以下方法
 public String toString()
 {
   String x = "Radius: " + this.radius +"\n";
   String y = "Area: " + getArea() + "\n";

   String z = x + y;
   return z + super.toString();
 }   

假设您希望调用父 class toString() 而不是子调用,这不是这种情况是正常的,因为您已经重写了子方法中的 toString() 方法class,您在 main() 方法中显式调用它。

子 class 是使用父控制器构造的事实不会影响 toString() 因为您正在显式调用子 class.[=11 的重写 toString() =]

如果您希望父 toString() 方法被执行,您可以调用并打印 super.toString() 而不是 c1.toString()。