我正在尝试使用多态性和继承性来制作框对象,我的用于设置宽度的吸气剂和吸气剂工作得很好,但是对于长度,我写了与宽度的吸气剂和吸气剂相同的格式(如果宽度或长度) set不会介于MIN(3)和MAX(20)之间,将变量设置为最接近的变量(例如,如果length为24,则将其设置为20,如果length为2,则将其设置为3),这对宽度来说是完美的,但对长度来说不是吗?我很困惑,我不知道为什么。这是我的代码。
Box.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)
答案 0 :(得分:4)
您在setLength
和setWidth
中遇到相同的错误(只是变量名称不同);也就是说,您已经测试了现有的length
(或width
)属性,而不是 new 值。另外,您需要逻辑和。更改
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;
}
}
答案 1 :(得分:1)
如果您希望变量超出边界,则使其最接近(MIN / MAX),则函数应更改为:
Private Sub Command20_Click()
If LastRecordP(Me) Then
DoCmd.GoToRecord , , acFirst
Else
DoCmd.GoToRecord , , acNext
End If
End Sub