扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
基类对象总是在任何派生类之前构造的。因此基类的构造函数在派生类的构造函数之前执行。如果基类有多个构造函数,派生类就可以决定要调用的构造函数。例如,我们可以修改我们的 Point 类来添加第二个构造函数:
public class Point
{
private int x, y;
public Point()
{
x = 0; y = 0;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
然后,通过使用 base 关键字,我们可以将 ColorPoint 类更改为使用某个特定的可用构造函数:
public class ColorPoint : Point
{
private Color color;
public ColorPoint(int x, int y) : base (x, y)
{
color = Color.Red;
}
}
在 Java 中,这项功能是通过 super 关键字来实现的。
查看本文来源如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。