Java getter和setter不能正常工作?

Java getter和setter不能正常工作?,java,getter-setter,Java,Getter Setter,我尝试使用多态性和继承来创建box对象,我的用于设置宽度的getter和setter工作得非常好,但是对于长度,我编写的格式与用于宽度的getter和setter相同,如果宽度或长度集不在最小值(3)和最大值(20)之间,将变量设置为最接近的值(例如,如果长度为24,则设置为20,如果长度为2,则设置为3)。无论如何,这对于宽度来说效果很好,但对于长度来说效果不好?我很困惑,我不知道为什么。这是我的密码 Box.java package l08; public class Box { /**

我尝试使用多态性和继承来创建box对象,我的用于设置宽度的getter和setter工作得非常好,但是对于长度,我编写的格式与用于宽度的getter和setter相同,如果宽度或长度集不在最小值(3)和最大值(20)之间,将变量设置为最接近的值(例如,如果长度为24,则设置为20,如果长度为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
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
setWidth
中有相同的错误(只是变量名称不同);也就是说,您已经测试了现有的
长度
(或
宽度
)属性,而不是新的值。此外,您还需要一个逻辑and。变,

public void setLength(int newLength) {
    if (length >= MIN || length <= MAX) {
        length = newLength;
    }
}

如果您希望变量在边界之外时接近(最小/最大),则函数应更改为:

public void setLength(int newLength) {
    if (newLength <= MIN) 
        length = MIN;
    else if(newLength >= MAX)
        length = MAX;
    else
        length = newLength;
}

public void setWidth(int newWidth) {
    if (newWidth <= MIN) 
        width = MIN;
    else if(newWidth >= MAX)
        width = MAX;
    else
        width = newWidth;
}
public void setLength(int newLength){
如果(新长度=最大值)
长度=最大值;
其他的
长度=新长度;
}
公共void setWidth(int newWidth){
如果(新宽度=最大值)
宽度=最大值;
其他的
宽度=新宽度;
}

还有一件事,代码不是推到最小值/最大值的缩写吗?也就是说,如果当前
length=10
并且我调用
setLength(1)
,函数将保持
length=10
,而不是将其更改为
length=3(MIN)
。这不是他的功能描述所说的,不是吗?但是这仍然有和我相同的输出,你正在考虑的多态性和继承性在哪里?问题是,所有的设置者所要做的就是改变长度或宽度,只要它在最小值和最大值之间,如果它们不在值中,就不要改变值,构造函数应该处理最小值和最大值,这是我的问题-这是一个赋值,我通常做这些事情都没有问题,但是这个开关很奇怪
public void setLength(int newLength) {
    if (newLength >= MIN && newLength <= MAX) {
        length = newLength;
    }
}
public void setWidth(int newWidth) {
    if (width >= MIN || width <= MAX) {
        newWidth = width;
    }
}
public void setWidth(int newWidth) {
    if (newWidth >= MIN && newWidth <= MAX) {
        width = newWidth;
    }
}
public void setLength(int newLength) {
    if (newLength <= MIN) 
        length = MIN;
    else if(newLength >= MAX)
        length = MAX;
    else
        length = newLength;
}

public void setWidth(int newWidth) {
    if (newWidth <= MIN) 
        width = MIN;
    else if(newWidth >= MAX)
        width = MAX;
    else
        width = newWidth;
}