Java 测试程序存在有关私有(private)访问的错误

标签 java class methods compiler-errors

我一直在创建一个类测试类型的程序用于学习目的,但我目前陷入如何修复我的源代码的困境。我宁愿有人解释得很好,因为我只想理解这个新手问题。

/*
* File: polygon.java
* Author: M. Morales
* Date: March 1, 2015
* Purpose: Sets the foundation for the polygon
* test
*/

public class polygon {

    // polygon class has 4 fields
    private int numSides;
    private double sideLength;
    private double xCoord;
    private double yCoord;

    // Default constructor
    public polygon () {
        numSides = 4;
        sideLength = 10.0;
        xCoord = 0.0;
        yCoord = 0.0;
    }

    // constructor
    public polygon (double psideLength, double px, double py, int pnumSides) {
        numSides = pnumSides;
        sideLength = psideLength;
        xCoord = px;
        yCoord = py;
    }

    // Setter methods
    // setnumSides
    private void setnumSides(int pnumSides) {
        numSides = pnumSides;
    }
    // setsideLength()
    private void setsideLength(double psideLength)  {
        sideLength = psideLength;
    }
    // setxCoord()
    private void setxCoord(double px)  {
        xCoord = px;
    }
    // setyCoord()
    private void setyCoord(double py)  {
        yCoord = py;
    }


    // Getter methods
    // getnumSides
    public double getnumSides() {
        return numSides;
    }
    // getsideLength
    public double getsideLength() {
        return sideLength;
    }
    // getxCoord
    public double getxCoord() {
        return xCoord;
    }
    // getyCoord
    public double getyCoord() {
        return yCoord;
    }

    // Use Perimeter method to get the distance around
    public double getperiMeter(polygon s1) {
        // perimeter
        double periMeter = Math.abs(s1.getnumSides() * s1.getsideLength());
        return periMeter;
    }


    // toString method
    public String toString() {
        String str = "(" + numSides + ", " + sideLength +  "," + xCoord + ","
        + yCoord + ")";
        return str;
    }

}

上面是第一部分,但测试是无法编译的

/*
* File: TestPolygon.java
* Author: M. Morales
* Date: March 1, 2015
* Purpose: creates simplistic polygon perimeter
* test
*/

public class TestPolygon2 {
    public static void main(String[] args)  {

        int numSides = 4;

        double sideLength = 10.0;

        double xCoord = 0.0;

        double yCoord = 0.0;


        //Construct a polygon
        polygon s1 = new polygon();


        s1.setnumSides(numSides);

        // Call the getter methods
        int s1numSides = s1.getnumSides();
        double s1sideLength = s1.getsideLength();
        double s1xCoord = s1.getxCoord();
        double s1yCoord = s1.getyCoord();
        // Print results
        System.out.println("s1 values from getnumSides() getsideLength()    getxCoord() getyCoord " + s1numSides + "," + s1sideLength + "," + s1xCoord + "," + s1yCoord);

        // Call the Perimeter Method
        double periMeter = s1.getperiMeter(s1);
        // Print results
        System.out.println("The perimeter of the polygon is: " +
        periMeter);

        // Change the value of s1
        // Using the setter method
        int newnumSides = 8;
        double newsideLength = 11.0;
        double newxCoord = 2.0;
        double newyCoord = 2.0;
        s1.setnumSides(newnumSides);
        s1.setsideLength(newsideLength);
        s1.setxCoord(newxCoord);
        s1.setyCoord(newyCoord);

        // Recalculate the Distance
        periMeter = s1.getperiMeter(s1);
        // Print results
        System.out.println("New perimeter is: " +
        periMeter);
        // Display the values using toString
        System.out.println(s1.toString());



    }
}
<小时/>

最佳答案

polygon 类的 set* 方法是私有(private),因此您无法从 TestPolygon2 调用它们> 类。您必须将它们更改为 public 才能调用它们。

关于Java 测试程序存在有关私有(private)访问的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28806589/

相关文章:

c# - UOM(度量单位)设计模式

java - Smack Presence 不起作用

javascript - jQuery 选择不包含类的元素

c++ - 在创建 C++ 类时遇到 if/else 问题

java - 无法在 Activity 内调用 View 对象的方法

java - 如何重写我的方法以返回列表或电影而不是字符串列表?

java - 识别当前正在处理的元素

java - 这个比较器类如何工作?

php - 在对象中使用对象?

java - 在 setOnAction 事件中调用方法并评估命令