如何解决组合、继承之类的问题?如何证明一个“圆”是一个“点”?
在这个(虚构的!)示例中,我正在画一个圆圈。
point
可以被封装,所以
class Point
{
private x,y;
public function construct(x,y)
{
this.x = x;
this.y = y;
}
public function getX()
{
return this.x;
}
public function getY()
{
return this.y;
}
}
function drawCircle(Point point,int radius)
{
if (checkInScreen(Point,radius))
{
}
}
function checkInScreen(Point point)
{
if (point.getX() > 0)// blah blah,and random checking
}
到目前为止一切都很好,但我看到 Point
和半径可以封装到 Circle
中!
class Circle
{
private Point p;
private int radius;
public function construct(Point p,int radius)
{
this.p = p;
this.radius = radius;
}
}
function drawCircle(Circle circle)
{
if (checkInScreen(circle)) // ***** FAIL
{
}
}
function checkInScreen(Point point)
{
if (point.getX() > 0) // blah blah,and random checking
}
现在它将无法编译,因为 checkInScreen
期望的是 Point
而不是 Circle
。没错,但是该怎么办?
当然我可以从圆圈中检索原点:
function drawCircle(Circle circle)
{
if (checkInScreen(circle.getPoint()))
{
}
}
和
class Circle
{
private Point p;
private int radius;
public function construct(Point p,int radius)
{
this.p = p;
this.radius = radius;
}
public function getPont()
{
return p; // VOILATION DEMETERS LAW!
}
}
但后来我违反了德墨忒尔定律!如何解决?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。