我知道在同一個類中從一種方法委託給另一種方法是可以的,因爲它減少了代碼重複,但是將調用委託給被認爲是不好實踐的其他類類型?委託類之間的調用不良做法?
例如:
這樣做沒問題。
double Point::GetDistanceFrom(const Point& point) const {
return GetDistanceFrom(this->GetX(), this->GetY(), point.GetX(), point.GetY());
}
double Point::GetDistanceFrom(const Point& one, const Point& two) {
return GetDistanceFrom(one.GetX(), one.GetY(), two.GetX(), two.GetY());
}
double Point::GetDistanceFrom(double x1, double y1, double x2, double y2) {
return std::sqrt(GetDistanceFromSquared(x1, y1, x2, y2));
}
double Point::GetDistanceFromSquared(double x1, double y1, double x2, double y2) {
x2 -= x1;
y2 -= y1;
return (x2 * x2 + y2 * y2);
}
double Point::GetDistanceFromSquared(const Point& one, const Point& two) {
return GetDistanceFromSquared(one.GetX(), one.GetY(), two.GetX(), two.GetY());
}
但這又如何呢?
double Point::GetDistanceFrom(const Line& line, bool isInfinite) const {
if(isInfinite) return line.ptLineDist(line.GetPointOne().GetX(), line.GetPointOne().GetY(), line.GetPointTwo().GetX(), line.GetPointTwo().GetY(), this->GetX(), this->GetY());
return line.ptSegDist(line.GetPointOne().GetX(), line.GetPointOne().GetY(), line.GetPointTwo().GetX(), line.GetPointTwo().GetY(), this->GetX(), this->GetY());
}
而這個?
double Line::GetDistanceFrom(const Point& point, bool isInfinite) const {
return point.GetDistanceFrom(*this, isInfinite);
}
對於投票結束這個問題的人 - 你會有什麼改進?這個問題是代碼審查和詢問OO校長之間的混合,但我個人認爲它有一個具體的答案。 –