Java超类toString()和构造函数调用[关闭]

问题描述 投票:1回答:3

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

Shape类:

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;

   }    
}

一个TestShape:

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
java super
3个回答
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
投票

您的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;
}

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.