学习C#语言时,经常会遇到选择C#构造函数问题,这里将介绍选择C#构造函数问题的解决方法。

选择C#构造函数
  
基类对象总是在任何派生类之前构造的。因此基类的构造函数在派生类的构造函数之前执行。如果基类有多个构造函数,派生类就可以决定要调用的构造函数。例如,我们可以修改我们的 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 类更改为使用某个特定的选择C#构造函数:
- public class ColorPoint : Point
 - {
 - private Color color;
 - public ColorPoint(int x, int y) : base (x, y)
 - {
 - color = Color.Red;
 - }
 - }
 
在 Java 中,这项功能是通过 super 关键字来实现的。
【编辑推荐】
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号