我是编码新手,目前正在使用 Java 进行 OOP 工作。我的老师让我们创建一个矩形类,而不是仅仅导入内置类。不管怎样,我们要创建以整数和双精度作为参数的构造函数。我的问题是,如果数据类型不同,有两个构造函数,我是否需要为每种类型使用单独的 getter / setter?这是我可以使用通用数据类型的时候吗?我已将我创建的内容包含在下面。谢谢。
public class Rectangle {
private int height;
private int width;
private double doubleHeight;
private double doubleWidth;
public Rectangle() {
height = 0;
width = 0;
}
public Rectangle(int h, int w) {
height = h;
width = w;
System.out.println("A Rectangle has been created using integer dimensions.");
}
public Rectangle(double h, double w) {
doubleHeight = h;
doubleWidth= w;
System.out.println("A Rectangle has been created using double dimensions.");
}
public int getIntHeight() {
return height;
}
public double getDoubleHeight() {
return doubleHeight;
}
public int setIntHeight(int h) {
height = h;
}
public double setDoubleHeight(double h) {
doubleHeight = h;
}
public int getIntArea() {
return width * height;
}
public double getDoubleArea() {
return doubleWidth * doubleHeight;
}
public int getIntPerimeter() {
return width*2 + height*2;
}
public double getDoublePerimeter() {
return doubleWidth*2 + doubleHeight*2;
}
public String toString() {
if (this.getIntHeight() >= 1) {
return "Height: " + height + ", Width: " + width;
}
else {
return "Height: " + doubleHeight + ", Width: " + doubleWidth;
}
}
}
是的,如果您希望两种类型都可访问,则需要不同的 getter。由于参数类型不同,您的 setter 可以具有相同的名称。如有必要,您可以将这些 getter/setter 强制转换为构造函数中使用的类型。例如:
public class Rectangle {
private int height;
private int width;
private double doubleHeight;
private double doubleWidth;
public Rectangle() {
this.height = 0;
this.width = 0;
}
public Rectangle(int h, int w) {
this.height = h;
this.width = w;
System.out.println("A Rectangle has been created using integer dimensions.");
}
public Rectangle(double h, double w) {
this.doubleHeight = h;
this.doubleWidth = w;
System.out.println("A Rectangle has been created using double dimensions.");
}
public int getIntHeight() {
if (this.height > 0)
return this.height;
else
return (int) getDoubleHeight();
}
public double getDoubleHeight() {
if (this.doubleHeight > 0)
return this.doubleHeight;
else
return (double) getIntHeight();
}
public void setHeight(int h) {
if (this.height > 0)
this.height = h;
else
setHeight((double) h);
}
public void setHeight(double h) {
if (this.doubleHeight > 0)
this.doubleHeight = h;
else
setHeight((int) h);
}
public int getIntArea() {
if (this.height > 0)
return this.width * this.height;
else
return (int) getDoubleArea();
}
public double getDoubleArea() {
if (this.doubleWidth > 0)
return this.doubleWidth * this.doubleHeight;
else
return (double) getIntArea();
}
public int getIntPerimeter() {
if (this.height > 0)
return this.width * 2 + this.height * 2;
else
return (int) getDoublePerimeter();
}
public double getDoublePerimeter() {
if (this.doubleHeight > 0)
return this.doubleWidth * 2 + this.doubleHeight * 2;
else
return (double) getIntPerimeter();
}
public String toString() {
if (this.height >= 1) {
return "Height: " + this.height + ", Width: " + this.width;
} else {
return "Height: " + this.doubleHeight + ", Width: " + this.doubleWidth;
}
}
}
您应该将高度和宽度存储为双精度数(因为双精度数比 int 宽,即 int 可以放入 double 但 double 不能放入 int 中),然后让带有 double 和 int 参数的构造函数简单地设置 double 高度和宽度的值。
无需使用通用数据类型。
它应该是这样的:
公共类矩形{
private double height;
private double width;
public Rectangle() {
height = 0;
width = 0;
}
public Rectangle(int h, int w) {
height = h;
width = w;
System.out.println("A Rectangle has been created using integer dimensions.");
}
public Rectangle(double h, double w) {
height = h;
width = w;
System.out.println("A Rectangle has been created using double dimensions.");
}
public double getHeight() {
return this.height;
}
public void setHeight(double h) {
this.height = h;
}
public double getWidth() {
return this.width;
}
public void setWidth(double w) {
this.width = w;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return width * 2 + height * 2;
}
public String toString() {
return "Height: " + height + ", Width: " + width;
}
}