【发布时间】:2015-03-10 19:52:43
【问题描述】:
假设我们有两个名为 Point 和 Line 的类。 Line 类有两个构造函数。这是Point类的代码。
// The Point class definition
public class Point {
// Private member variables
private int x, y; // (x, y) co-ordinates
// Constructors
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() { // default (no-arg) constructor
x = 0;
y = 0;
}
}
这是 Line 类的代码。
public class Line {
// Private member variables
Point begin, end; // Declare begin and end as instances of Point
// Constructors
public Line(int x1, int y1, int x2, int y2) {
begin = new Point(x1, y1);
end = new Point(x2, y2);
}`
public Line(Point begin, Point end) {
this.begin = begin;
this.end = end;
}
}
如你所见 Line 类有两个构造函数。第一个构造函数是 Compositon 的示例,而第二个构造函数是聚合的示例。现在,对于这个案例,我们能说些什么呢?一个类可以同时具有聚合和组合吗?感谢您的回答。
【问题讨论】:
标签: java constructor uml aggregation composition