问题描述
所以我需要由2个类组成的代码,已经形成的方法点和包括用于创建矩形本身的构造函数的方法矩形,矩形本身应通过此测试形成
@Test
public void testRectangle2() {
Rectangle rect = new Rectangle(20,30,20,20);
assertAll(
() -> assertEquals(10,rect.getTopLeft().getX()),() -> assertEquals(20,rect.getTopLeft().getY()),() -> assertEquals(30,rect.getBottomright().getX()),() -> assertEquals(40,rect.getBottomright().getY()),rect.getWidth(),20),rect.getHeight(),20)
);
}
Class Point可以正常工作,我只是为了清楚起见对它进行了
public class Point {
private int x,y;
public Point(int x,int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0,0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveto(int newX,int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx,int dy) {
x += dx;
y += dy;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
这是Rectangle本身的类,它同时包含构造函数和附加方法。
public class Rectangle {
public int width = 0;
public int height = 0;
public Point center;
public int xCenter;
public int yCenter;
public Point getTopLeft() {
Point point = new Point(center.getX(),center.getY());
point.moveRel(- width / 2,- height / 2);
return point;
}
public Point getBottomright() {
Point point = new Point(center.getX(),center.getY());
point.moveRel(width / 2,height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Point getCenter() {
Point center = new Point(xCenter,yCenter);
return center;
}
public Rectangle(int xCenter,int yCenter,int width,int height) {
this.xCenter=xCenter;
this.yCenter=yCenter;
this.width=width;
this.height=height;
}
}
问题在它自己的构造函数中,它应该从测试中获取值,然后形成矩形,但是它没有这样做。
解决方法
此代码中有几个问题。
构造函数的问题是未创建中心点,然后测试以NullPointerException
中的getTopLeft/getBottomRight
失败,无论哪个先调用,因为这些方法不使用getCenter()
方法而是引用{ {1}}字段。
因此,仅在上述方法中使用上述getter并删除冗余字段center
就足够了。
center
但是,此设计具有某些缺陷,class Rectangle {
public int width = 0;
public int height = 0;
public int xCenter;
public int yCenter;
public Point getTopLeft() {
Point point = getCenter();
point.moveRel(- width / 2,- height / 2);
return point;
}
public Point getBottomRight() {
Point point = getCenter();
point.moveRel(width / 2,height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Point getCenter() {
Point center = new Point(xCenter,yCenter);
return center;
}
public Rectangle(int xCenter,int yCenter,int width,int height) {
this.xCenter=xCenter;
this.yCenter=yCenter;
this.width=width;
this.height=height;
}
}
字段是公共的,并且可以从外部更改,从而破坏了封装原理。此外,尽管可以重复使用,也会创建冗余点。
下面是Rectangle类的改进版本:
- “不可变”字段
- 延迟初始化
Rectangle
和topLeft
点
rightBottom