主JAVA中显示方法未连接

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

我不知道如何从形状类调用显示方法到Main方法。这是一个作业,我们不应该更改 Main 中编写的内容,因此问题必须来自 shape 类。所有评论均直接来自说明。这是用 Java 完成的。

我已经被这个问题困扰了好几个小时了,无法弄清楚。如果有人可以提供帮助,我将非常感激。

(另外,这是我第一次使用这个网站,所以如果我搞砸了,lmk)

编辑:该错误具体出现在 obj.display() 和 obj1.display() 的 Main 方法中。该错误显示“无法解析方法‘display()’”。它推荐“void display(String)”和“String display(String, String)”,但是当我尝试这些时,它们都不起作用。

class Shape {
    // declare String variable for result and double to store area
    String result;
    double area;

    public String display(String res, String result) {
        // display method should add res parameter to result and return the result
        return result += res;
    }

    public double area() {
        //area method should initialize area to 0
        return area = 0;
    }

    static double area(double area) {
        return area = 0;
    }
}

    //create a child class triangle for shape
    class Triangle extends Shape {
        //declare variable length and width
        double length;
        double width;

        public Triangle(double length, double width) {
            //initialize length and width inside constructor and then call area method
            this.length = length;
            this.width = width;

            area();
        }
            // override area method to calculate area of triangle
            public double area() {
                return length * width / 2;
            }

            //override display method which returns the output of parent class display method
            public void display(String res){
                super.display(res, result);
            }

            //implement toString method to return the class name
            public String toString(){
                return this.getClass().getSimpleName();
            }
    }


    //create a child class circle for shape
    class Circle extends Shape{
        //declare a constant and initialize pi value
        double c = Math.PI;
        //declare variable radius
        double radius;

        public Circle(double radius) {
            //initialize radius inside constructor
            this.radius = radius;
        }

        //and then call area method
        public double area(){
            // override area method to calculate area of circle
            return c*Math.pow(radius, 2);
        }

        //override display method which returns the output of parent class display method
        void display(String res){
            super.display(res, result);
        }

        //implement toString method to return the class name
        public String toString(){
            return this.getClass().getSimpleName();
        }
    }
public class Main {
    public static void main(String[] args) {
        Triangle obj= new Triangle(10,20);
        System.out.println("The area of "+obj+" With dimensions "+obj.length +" and "+obj.width+" is: " + obj.display());

        Circle obj1= new Circle(10);
        System.out.println("The area of "+obj1+" With radius "+obj1.radius +" is: "+obj1.display());
    }
}

我尝试按照给出的错误消息和自动建议进行操作,但它们没有什么作用。我将其从 void 更改为 public String 并尝试在不同的类中移动该方法,但无济于事。

java methods polymorphism overloading
1个回答
0
投票

我想通了!我必须将显示方法内容更改为这个vvv

            public String display(){
            return Double.toString(area());
        }
© www.soinside.com 2019 - 2024. All rights reserved.