【问题标题】:Java Test Program with errors about private access带有私有访问错误的 Java 测试程序
【发布时间】:2015-03-02 09:42:05
【问题描述】:

我一直在创建一个用于学习目的的类测试类型的程序,但我目前被困在如何修复我的源代码上。我宁愿有人解释清楚,因为我只想了解这个新手问题。

/*
* 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());



    }
}

【问题讨论】:

  • 您遇到的错误是什么?此外,修正缩进以获得更好的可读性。
  • 从双精度到整数的可能有损转换。另外,我设置的东西在多边形中具有私有访问权限

标签: java class methods compiler-errors


【解决方案1】:

polygon 类的 set* 方法是 private,因此您不能从 TestPolygon2 类中调用它们。您必须将它们更改为 public 才能调用它们。

【讨论】:

  • 将它们设为私有包就足够了(假设测试在同一个标​​准包中)。
  • @assylias 这取决于polygon 实例是否应该能够被其包外的类所改变
  • 这个特定问题的重点是在它们是私人的时候操纵它们。如果他们是公开的,那就容易多了。
  • @MarioMorales 如果不允许更改 setter 方法的访问修饰符,则根本无法从 TestPolygon2 类中调用它们(不包括反射技巧)。
  • 感谢您的提醒。我想有些事情是你不能做的。虽然现在这个反射技巧引起了我的兴趣哈哈
猜你喜欢
  • 2011-10-09
  • 2015-03-07
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 2015-02-20
相关资源
最近更新 更多