toString() 不适用于多级继承

问题描述 投票:0回答:0

我有以下java类:

Shape
   Circle
   Rectangle
      Square

Shape是父类,Circle和Rectangle扩展了Shape和Square 扩展矩形。我正在尝试打印以下内容:

Circle with radius: 54.37, Centered at X: 9.29 Y: 0.17
Has area: 9287.38
Has circumference: 172.81

Rectangle with height: 70.38 and width: 59.78, Centered at X: 80.07 Y: 53.59
Has area: 4207.50
Has perimeter: 260.33

Square with side: 5.00, Centered at X: 9.00 Y: 3.73
Has area: 25.00
Has perimeter: 20.00

The output that I am getting is:

Circle with radius: 54.37, Centered at X: 9.29 Y: 0.17
Has area: 9287.38
Has circumference: 172.81

Rectangle with height: 70.38 and width: 59.78, Centered at X: 80.07 Y: 53.59
Has area: 4207.50
Has perimeter: 260.33

Square with side: 0.00, Centered at X: 92.88 Y: 44.97Square with side: 5.00, Rectangle 
with height: 5.00 and width: 5.00
Has area: 0.00
Has perimeter: 0.00
Has area: 25.00
Has perimeter: 20.00

我意识到自从 Square 调用 super 我一直附加到我的 StringBuilder 解释了一些 Square 输出。我不 理解为什么边长为 0.00 的 Square 会发生,我是 不知道如何解决这个问题。关于更好的任何建议 更好的设计方式值得赞赏:这是代码:

import java.util.ArrayList;
public class Shape{
    private double x;
    private double y;
    private StringBuilder str0 = new StringBuilder();
    private StringBuilder str1 = new StringBuilder();
    private StringBuilder str2 = new StringBuilder();

    public Shape(double x, double y){
        this.x = x;
        this.y = y;
    }
    public static void main(String[] args){
        System.out.println('\u000C');

        ArrayList<Shape> list = new ArrayList<Shape>();
        double x;
        double y;
        double radius;
        double height;
        double width;
        double side;

        for (int i = 0; i < 12; i++){
            x = Math.random() * 100;
            y = Math.random() * 100;

            if (i % 3 == 0){
                radius = Math.random() * 100;
                Circle z = new Circle(x, y, radius);
                list.add(z);                
            }else if (i % 3 == 1){
                width = Math.random() * 100;
                height = Math.random() * 100;
                Rectangle z = new Rectangle(x, y, height, width);
                list.add(z);
            }else if (i % 3 == 5){
                side = Math.random() * 100;

                Square z = new Square(x, y, side);
                list.add(z);
            }
        }
        
        x = Math.random() * 100;
        y = Math.random() * 100;
        side = Math.random() * 100;
        Square z = new Square(x, y, side);
        list.add(z);

        for(Shape s : list){
            System.out.println(s.getStr0());
            System.out.println(s.getStr1());

        }    
    }
    public String toString(){
        String x = String.format("%.2f",this.x);
        String y = String.format("%.2f",this.y);
        return "Centered at X: " + x + " Y: " + y ;
    }    
    public double calculateArea(){
        return -999.99;
    }    
    public double calculateCircumference(){
        return -999.99;
    }
    public double calculatePerimeter(){
        return -999.99;
    }    
    public double getX(){
        return this.x;
    }
    public void setX(double x){
        this.x = x;
    }
    public double getY(){
        return this.y;
    } 
    public void setY(double y){
        this.y = y;
    }
    public StringBuilder getStr0(){
        return this.str0;
    } 
    public void setStr0(StringBuilder str0){
        this.str0 = str0;
    } 
    public StringBuilder getStr1(){
        return this.str1;
    } 
    public void setStr1(StringBuilder str1){
        this.str1 = str1;
    }
    public String findCircle(ArrayList<Shape> list){
        String text="";
        for (Shape s : list){
            if (s instanceof Circle){
                text+= s.getStr0() + "\n\n";
            }

        }
        return text;
    }        
}
public class Rectangle extends Shape{
    private double height;
    private double width;
    
    public Rectangle(double x, double y, double height, double width){
        super(x, y);
        this.height = height;
        this.width = width;
        
        String area = String.format("%.2f",this.calculateArea());
        String perimeter = String.format("%.2f",this.calculatePerimeter());
        
        getStr0().append(toString() + ", " + super.toString() );
        System.out.println("99: " + getStr0());
        getStr1().append("Has area: " + area + "\n");
        getStr1().append("Has perimeter: " + perimeter + "\n");
        
    }
    public double calculateArea(){
        return height * width;
    }
    public double calculatePerimeter(){
        return 2 * height + 2 * width;
    }
    public String toString(){
        String text = super.toString();
        String height = String.format("%.2f",this.height);
        String width = String.format("%.2f",this.width);        
        return "Rectangle with height: " + height + " and width: " + width;
    }
}
public class Circle extends Shape{
    private double radius;
    
    public Circle(double x, double y, double radius){
        super(x, y);
        this.radius = radius;
        
        String area = String.format("%.2f",this.calculateArea());
        String circumference = String.format("%.2f",this.calculateCircumference());        
        
        getStr0().append(toString() + ", " + super.toString());
        getStr1().append("Has area: " + area + "\n");
        getStr1().append("Has circumference: " + circumference + "\n");
    }
    
    public double calculateCircumference(){
        return 2 + Math.PI * radius;
    }
    public double calculateArea(){
        return Math.PI * Math.pow(radius,2);
    }
    public String toString(){       
        String radius = String.format("%.2f",this.radius);
        
        return "Circle with radius: " + radius;
    }
}
public Square(double x, double y, double side){
        super(x, y, side, side);
        this.side = side;

        String area = String.format("%.2f",this.calculateArea());
        String perimeter = String.format("%.2f",this.calculatePerimeter());

        System.out.println("1: " + toString());
        System.out.println("2: " + getStr0());
        System.out.println("3: " + getStr1());

        getStr0().append(toString() + ", " + super.toString() );

        getStr1().append("Has area: " + area + "\n");
        getStr1().append("Has perimeter: " + perimeter + "\n");
        
        System.out.println("4: " + toString());
        System.out.println("5: " + getStr0());
        System.out.println("6: " + getStr1());
    }

    public String toString(){

        String side = String.format("%.2f",this.side);

        return "Square with side: " + side ;
    }

    public double calculateArea(){
        return side * side;
    }

    public double calculatePerimeter(){
        return 4 * side;
    }
}
java inheritance multi-level
© www.soinside.com 2019 - 2024. All rights reserved.