用 Java 封装

Encapsulation with Java

作为某些课程作业的一部分,我正在尝试在程序中实现封装,但是我 运行 遇到了一个错误,我似乎无法以我有限的知识修复它我的 Teacher/Lecturer 帮助我,他非常擅长他所做的事情,但是在实际传达信息时做得不是很好,因此有人可以帮助我解决以下出现的错误编程并向我解释为什么它没有按预期工作。

class TwoDShapeEncap{
    double width, height;

    //Width
    void setWidth(double w){
        width = w;
    }

    double getWidth(){
        return width;
    }
    //Height
    void setHeight(double h){
        height = h;
    }

    double getHeight(){
        return height;
    }
}

class Triangle extends TwoDShapeEncap{
    String type;
    private double sideA, sideB, sideC, adjacent, opposite;

    //Side A
    void setsideA(double a){
        sideA = a;
    }
    double getsideA(){
        return sideA;
    }
    //Side B
    void setsideB(double b){
        sideB = b;
    }
    double getsideB(){
        return sideB;
    }
    //Side C
    void setsideC(double c){
        sideC = c;
    }
    double getsideC(){
        return sideC;
    }
    //Adjacent
    void setadjacent(double a){
        adjacent = a;
    }
    double getadjacent(){
        return adjacent;
    }
    //Opposite
    void setopposite(double o){
        width = o;
    }
    double getopposite(){
        return opposite;
    }

    double getPerimeter(){
        if(getsideB() == 0.0 && getsideC() == 0.0){
            type = "equilateral";
            return getsideA() * 3;
        }
        else if (getsideC() == 0.0){
            type  = "isosceles";
            return getsideA() + getsideB() * 2;
        }
        else{
            type = "scalene";
            return getsideA() + getsideB() + getsideC();
        }
    }


    //*******************************************************************************************
    //* Paste the perimeter() and hypotenuse() methods from your previous class into this class *
    //*******************************************************************************************

    //***************************************
    //* add an area method()into this class *
    //***************************************

    double area(double a, double b){
        getWidth();
        getHeight();
        return (getWidth() * getHeight()/2);
    }
}

class Rectangle extends TwoDShapeEncap{
    boolean issquare;

    private double height, width;

    //Height
    void setHeight(double h){
        height = h;
    }
    double getHeight(){
        return height;
    }
    //Width
    void setWidth(double w){
        width = w;
    }
    double getWidth(){
        return width;
    }

    double perimeter(double h, double w){
        getHeight();
        getWidth();
        return getHeight() * 2 + getWidth() * 2;
    }

    double area(double a, double b){
        //getWidth();
        //getHeight();
        return (getWidth() * getHeight()/2);
    }

    boolean testSquare(double h, double w){
        //getHeight();
        //getWidth();
        if (getHeight() == getWidth())
            issquare = true;
        else issquare = false;
        return issquare;
    }

    //*********************************************
    //* add area and perimeter methods this class *
    //*********************************************

    //*************************************************************************
    //* add a testSquare method to test if a particular rectangle is a square *
    //*************************************************************************

}

//Add a circle class which includes area and circumference methods

class Circle extends TwoDShapeEncap{
    double radius, diameter;

    double area (double r){
        radius = r;
        return Math.PI * (radius * radius);
    }

    double perimeter (double r){
        radius = r;
        return 2 * (Math.PI * radius);
    }
}

class TwoDShapeEncapDemoNew {
    public static void main(String args[]) {
    //Triangle
    Triangle t = new Triangle();
    t.setsideA(5.7);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA() );
        System.out.println("The type is " + t.type);
        System.out.println();
    t.setsideB(7.3);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA() );
        System.out.println("If sideB is " + t.getsideB() );
        System.out.println("The type is " + t.type);
        System.out.println();
    t.setsideC(2.7);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA());
        System.out.println("If sideB is " + t.getsideB());
        System.out.println("If sideC is " + t.getsideC());
        System.out.println("The type is " + t.type);
        System.out.println();
        //Rectangle
        Rectangle r = new Rectangle();
    r.setHeight(7.8);
    r.setWidth(4.2);
        System.out.println("The perimeter is " + r.perimeter());
        System.out.println("The");

    }
}

错误信息:

Main.java:186: error: method perimeter in class Rectangle cannot be applied to given types; System.out.println("The perimeter is " + r.perimeter()); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length 1 error –

当你打电话时:

System.out.println("The perimeter is " + r.perimeter());

在r.perimeter中你必须传递两个参数(如你的签名所希望的)

你在矩形中的方法class:

double perimeter(double h, double w){
    getHeight();
    getWidth();
    return getHeight() * 2 + getWidth() * 2;
}

所以修复:

System.out.println("The perimeter is " + r.perimeter(r.getHeight(), r.getWidth()));

您还可以在没有参数的情况下修复您的方法边界,因为在正文中您使用了 getHeigth() 和 getWidth() 属性

所以你可以这样写:

double perimeter(){
    return getHeight() * 2 + getWidth() * 2;
}

class Rectangle 中的方法 perimeter 需要两个参数,而您没有传递任何参数。你可以这样称呼它:

r.perimeter(7.8,4.2);

或者重新定义方法,使其看起来像这样:

double perimeter(){
    return getHeight() * 2 + getWidth() * 2;
}

那是因为您定义的 perimeter 函数是这样的:

double perimeter(double h, double w){
    getHeight();
    getWidth();
    return getHeight() * 2 + getWidth() * 2;
}

并在不带参数的情况下调用 System.out.println("The perimeter is " + r.perimeter());

因为你并不是真的在无意义地使用 double hdouble w,你只需要从方法定义中删除它们

double perimeter(){
    return getHeight() * 2 + getWidth() * 2;
}

修改您的签名以删除参数。

class Rectangle extends TwoDShapeEncap{
    ///...

    double perimeter(double h, double w){
        getHeight();
        getWidth();
        return getHeight() * 2 + getWidth() * 2;
    }

应该是

class Rectangle extends TwoDShapeEncap{
    ///...

    double perimeter(){
        //Notice that you don't need to pass in these arguments 
        //as this function gets these arguments by itself. 
        return getHeight() * 2 + getWidth() * 2;
    }

由于每个人都只面临参数问题,所以我将面对这个问题:如果您是 "outside" 您的 class,Getter 用于获取私有字段的值!如果您在 class 中的方法中,则不必使用吸气剂,您可以只使用变量本身:

示例:

public class SomeClass {
    private int a;

    public void setA(int anotherA) {
        a = anotherA;
    }
    public int getA() {
        return a;
    }

    public int getSquareOfA() {
        // You don't use getA() to get the value now 
        // but you use a itself!
        return a*a;    // instead of 'return getA() * getA();'
    }
}

您的代码中确实有几个地方存在该问题!

根据你的问题:

您的问题是您调用的方法有 2 个参数而没有任何输入参数!

您可以删除方法的参数(在您的情况下这在逻辑上是正确的做法),或者您传递一些参数。

在您的特定情况下,这意味着,按如下方式更改您的 perimiter() 方法:

double perimiter() {
    return (height + width) * 2;
    // or if you want to impress your teacher ;) :
    // return (height + width) << 1
}

您还应该将该方法名称更改为 getPerimiter() 以跟上您自己的命名约定!