java - 获取器和设置器无法正常工作?
问题描述
我试图使用多态性和继承来制作盒子对象,我用于设置宽度的 getter 和 setter 工作得很好,但是对于长度,如果宽度或长度设置不下降,我将它写成与宽度的 getter 和 setter 相同的格式在 MIN 为 3 和 MAX 为 20 之间,将变量设置为最接近的值(例如,如果长度为 24,则设置为 20,如果长度为 2,则设置为 3)无论如何,这很有效宽度可以,长度不行?我很困惑,我不知道为什么。这是我的代码。
盒子.java
package l08;
public class Box {
/** Minimum size for either side of the box */
public static final int MIN = 3;
/** Maximum size for either side of the box */
public static final int MAX = 20;
/** The width of the box */
private int width;
/** The length of the box */
private int length;
/** * A symbol to be used to draw a box */
private char symbol;
/** Boolean to indicate whether the box is filled or not */
private boolean filled;
/**
* Constructor
*
* @param startWidth
* @param startLength
* @param startSymbol
* @param startFilled
*/
public Box(int startWidth, int startLength, char startSymbol,
boolean startFilled) {
if (length < MIN) {
length = MIN;
}
if (length > MAX) {
length = MAX;
}
this.width = startWidth;
this.length = startLength;
this.symbol = startSymbol;
this.filled = startFilled;
if (width < MIN) {
width = MIN;
}
if (width > MAX) {
width = MAX;
}
}// end constructor
/**
* Draw this box.
*/
public void drawBox() {
for(int star = 3; star < getLength(); star++)
System.out.print(getSymbol());
System.out.print("\n"+getSymbol());
for(int space = 0; space < getLength()-2; space++) System.out.print("
");
System.out.print(getSymbol() + "\n");
for(int star = 0; star < getLength(); star++)
System.out.print(getSymbol());
System.out.println();
}
/**
* Get the value of filled
*
* @return the value of filled
*/
public boolean isFilled() {
return filled;
}
/**
* Set the value of filled
*
* @param newFilled new value of filled
*/
public void setFilled(boolean newFilled) {
this.filled = newFilled;
}
/**
* Get the value of symbol
*
* @return the value of symbol
*/
public char getSymbol() {
// Activity 3, implement the getters
return symbol;
}
/**
* Set the value of symbol
*
* @param newSymbol new value of symbol
*/
public void setSymbol(char newSymbol) {
this.symbol = newSymbol;
}
/**
* Get the value of length
*
* @return the value of length
*/
public int getLength() {
return length;
}
/**
* Set the value of length
*
* @param newLength new value of length
*/
public void setLength(int newLength) {
if (length >= MIN || length <= MAX) {
length = newLength;
}
}
/**
* Get the value of width
*
* @return the value of width
*/
public int getWidth() {
// Activity 3, implement the getters
return width;
}
/**
* Set the value of width
*
* @param newWidth new value of width
*/
public void setWidth(int newWidth) {
if (width >= MIN || width <= MAX) {
newWidth = width;
}
}
}// end class
BoxDriver.java
package l08;
import java.util.Scanner;
public class BoxDriver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
//Testing constructors, if everything is good, then nothing will be
//printed!
System.out.println("\n\n=========================");
System.out.println("Testing the constructor");
System.out.println("=========================");
Box b1 = new Box(30,15,'*',true);
Box b2 = new Box(-2,-2,'#',false);
Box b3 = new Box(2,5,'*',false);
System.out.println("\n(If nothing is printed, "
+ "there were no run-time errors!)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing the getters of the Box class
System.out.println("\n\n=========================");
System.out.println("Testing the getters");
System.out.println("=========================");
System.out.println("Box 1: width=" + b1.getWidth()
+ " and length=" + b1.getLength()
+ "\t\t(should be 20 and 15)");
System.out.println("Box 2: symbol=" + b2.getSymbol()
+ " and filled=" + b2.isFilled() + "\t(should be # and false)");
System.out.println("Box 3: width=" + b3.getWidth()
+ " and length=" + b3.getLength() + "\t\t(should be 3 and 5)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing the setters of the Box class
System.out.println("\n\n=========================");
System.out.println("Testing the setters");
System.out.println("=========================");
b1.setFilled(false);//changing it to non-filled box
b1.setWidth(500);
b1.setLength(14);
b1.setSymbol('A');
System.out.println("Box 1: width=" + b1.getWidth()
+ " and length=" + b1.getLength() + " "
+ "symbol=" + b1.getSymbol()
+ " filled=" + b1.isFilled()
+ "\n(should now be changed to 20, 14, A, and false)");
b2.setFilled(true);//changing it to non-filled box
b2.setWidth(2);
b2.setLength(2);
System.out.println("\nBox 2: width=" + b2.getWidth()
+ " and length=" + b2.getLength() + " "
+ "symbol=" + b2.getSymbol()
+ " filled=" + b2.isFilled()
+ "\n(should now be changed to 3, 3, #, and true)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing drawBox() method
System.out.println("\n\n=========================");
System.out.println("Testing drawBox() method");
System.out.println("=========================");
System.out.println("Drawing box #1 (" + b1.getWidth() + "x"
+ b1.getLength() + ", not filled)");
b1.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
System.out.println("\n\nDrawing box #2 (" + b2.getWidth() + "x"
+ b2.getLength() + ", filled)");
b2.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
System.out.println("\n\nDrawing box #3 (" + b3.getWidth() + "x"
+ b3.getLength() + ", not filled)");
b3.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
}//end main()
}//end class
box2 的长度应该设置为 3,因为它是 2,但它不起作用?我真的不知道为什么。
输出
=========================
Testing the constructor
=========================
(If nothing is printed, there were no run-time errors!)
Press ENTER to continue...
=========================
Testing the getters
=========================
Box 1: width=20 and length=15 (should be 20 and 15)
Box 2: symbol=# and filled=false (should be # and false)
Box 3: width=3 and length=5 (should be 3 and 5)
Press ENTER to continue...
=========================
Testing the setters
=========================
Box 1: width=20 and length=14 symbol=A filled=false
(should now be changed to 20, 14, A, and false)
Box 2: width=3 and length=2 symbol=# filled=true
(should now be changed to 3, 3, #, and true)
解决方案
setLength
您在and中有相同的错误setWidth
(只是变量名称不同);也就是说,您已经测试了现有length
(或width
)属性而不是新值。此外,您需要一个逻辑与。改变,
public void setLength(int newLength) {
if (length >= MIN || length <= MAX) {
length = newLength;
}
}
到
public void setLength(int newLength) {
if (newLength >= MIN && newLength <= MAX) {
length = newLength;
}
}
和改变
public void setWidth(int newWidth) {
if (width >= MIN || width <= MAX) {
newWidth = width;
}
}
也真的要更新width
了……
public void setWidth(int newWidth) {
if (newWidth >= MIN && newWidth <= MAX) {
width = newWidth;
}
}
推荐阅读
- c# - 将 JSON 字符串转换为 C# 对象
- express - 快递路线不落在'/_api/'上,而是落在'/*'上?
- javascript - 避免通过遍历 JSON 对象获取数组索引
- azure-logic-apps - 用于部署 Azure 逻辑应用本地 ARM 模板的 Azure CLI 命令
- java - 尝试将 recylerview 添加到片段时找不到资源异常
- .net - 如何通过数据库创建/升级自动化 IIS 配置和托管?
- python - 在 Kivy ScreenManager 中实现手风琴到 SecondScreen
- android - 是否可以创建一个包含名称的类的对象?
- hadoop - 不同命令的不同 Hive 位置
- python - 如何在 Python 中调用 Web 服务函数?