接口和继承编译时错误

时间:2014-08-24 22:45:16

标签: java object inheritance methods interface

我编写了一个简短的程序,它使用继承和接口关系来计算用户所选形状的面积和周长。我尝试编译以下类时收到编译时错误。

班级广场

 public class Square extends Quadrilateral
        {

        double side1 =this.side1; 
        double side2 = this.side2; 
        double perimeter = this.perimeter; 
        double area = this.area; 

        Square(double instanceSide1, double instanceSide2) {
        side1 = instanceSide1;
        side2 = instanceSide2;
        }

        @Override
        public double area() 
        {
        area = side1 *side2;
        return area;
        }

        @Override
        public double perimeter() //math equation for determing perimeter
        {
        this.perimeter = (side1 * 2) + (side2 * 2) ;
        return perimeter;
        }
        }

这是我的Quad类

    public abstract class Quadrilateral implements Polygon{
    }

这是Polygon Class

    public interface Polygon {
    abstract void area();
    abstract void perimeter();    
    }

这是我为运行代码而构建的Tester Class

    public static void main(String[] args) //Constructor initalizing main class
    {
    int numberSides;
    int length;
    int base;

    Scanner sides = new Scanner(System.in); //Initializing Scanner Class


     /**
     * Do/while loop for selecting a 3 or 4 sided object
     */
    do 
    {
        System.out.println("Do you want a 3 or 4 sided shape? (Type either "
                + "3 or 4).");

        numberSides = sides.nextInt();

    } while (numberSides < 3 || numberSides > 4);

    if (numberSides == 3) {
        System.out.println("How long are the sides that are the same lenth?");
        length = sides.nextInt();
        System.out.println("How wide is the base? (whole numbers");
        base = sides.nextInt();
        IsoscelesTriangle Isoc = new IsoscelesTriangle(length, base);
        System.out.println("The area of the isocolese triangle is: " + Isoc.area());
        System.out.println("The perimeter of the isocolese triangle is: " + Isoc.perimeter());
    } else {
        System.out.println("How long are the sides are the same?");
        length = sides.nextInt();
        System.out.println("How wide is the base?");
        base = sides.nextInt();

        if (length == base) {
            Square Quad = new Square(length, base);
            System.out.println("The area of the square is: " + Quad.area());
            System.out.println("The perimeter of the square is: " + Quad.perimeter());
        } else {
            Rectangle Quad = new Rectangle(length, base);
            System.out.println("The area of the rectangle is: " + Quad.area());
            System.out.println("The perimeter of the rectangle is: " + Quad.perimeter());
        }
        }
     }

     }

1 个答案:

答案 0 :(得分:1)

实现接口的类没有与接口匹配的方法。接口的方法返回void,而类返回double。它们必须完全匹配,并且可能是正确的类和接口错误 - 将接口方法声明更改为返回double。

将来,如果您对它们有疑问,则需要发布所有错误/异常消息。它将帮助我们节省时间并为您提供更好的答案。

相关问题