【发布时间】:2015-02-11 08:29:59
【问题描述】:
我有 2 节课:
public class LineGeometry
{
public LineGeometry(Point startPoint, Vector direction)
{
this.startPoint = startPoint; this.direction = direction;
}
}
public class LineSegmentGeometry : LineGeometry
{
Point endPoint;
public LineSegmentGeometry(Point startPoint, Point endPoint, Vector direction) : base (startPoint, direction)
{
this.startPoint = startPoint; this.direction = direction; this.endPoint = endPoint;
}
}
基本上,我希望在LineSegmentGeometry 中再添加一个构造函数,如下所示:
LineSegmentGeometry(Point endPoint, LineGeometry l)
{
this.startPoint = l.startPoint;
this.direction = l.direction;
this.endPoint = endPoint;
}
因为本质上LineSegmentGeometry 与其基类完全相同,只是多了一个变量。
但是编译器会抛出一个错误,即基类由于其保护级别而无法访问。这种声明构造函数的方式是个好主意吗?如果可以,我该如何解决错误?
【问题讨论】:
-
顺便说一句,我强烈建议坚持每行一个语句。它使阅读更容易。此外,目前您的类声明无效(您不想要那些
())并且您的LineSegmentGeometry类不是从LineGeometry派生的。 -
感谢编辑错误
标签: c# inheritance