Java超类toString()和构造函数调用

时间:2018-03-01 21:49:46

标签: java super

我知道有类似的帖子,但它们都不适合我试图做的。我是一个较新的程序员,刚开始阅读Java的超类。我创建了一个名为shape的超类。然后我创建了一个名为circle的子类。我在这里难倒两部分,从我的子类toString方法调用我的超类中的toString方法,以及从我的子类到我的超类调用我的一个构造函数。我在下面提供了我的超类,子类和主要测试方法。

Shape类:

jdk-11

The Circle:

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;

   }
}

TestShape:

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

3 个答案:

答案 0 :(得分:2)

您的Circle课程会覆盖父toString方法的行为,并且不会将父结果与组合。因此,您只能看到Circle#toString方法计算的内容:

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

即半径和面积。

如果您想要添加父母结果,您需要使用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;
}

答案 1 :(得分:1)

你重写了toString()方法,但你没有调用超类中的方法。您需要在子类中使用以下方法。

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

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

答案 2 :(得分:0)

假设您希望调用父类toString()而不是子类,那么在子类中重写了toString()方法是明确的,这是正常的,您明确调用它在你的main()方法中。

使用父控制器构造子类的事实不会影响toSt​​ring(),因为您正在显式调用子类的重写的toString()。

如果你想让父toString()方法成为执行的方法,你可以调用并打印super.toString()而不是c1.toString()。

相关问题