gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:51:16 25 4
gpt4 key购买 nike

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

/*
* 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com