public class Ractangle {
private double width;
public double height;
public double Width {
set {
if (0 < value) {
this.width = value;
}
else {
this.width = value;
}
}
get {
return this.width;
}
}
public double Height {
set {
if (0 < value) {
this.height = value;
}
else {
this.height = -1;
}
}
get {
return this.Height;
}
}
public Ractangle(double w, double h) {
if (0 < w && 0 < h) {
this.width = w;
this.height = h;
}
}
}
//这里报错:Page180Ex04.Ractangle' does not contain a constructor that takes 0 arguments
public class ExRactangle:Ractangle {
public ExRactangle(double w, double h,int a) {
if (0 < w && 0 < h) {
base.Width = w;
base.Height = h;
}
}
public double CalcRactangeArea() {
return this.Height * this.Width;
}
}
//也就是说,我不知道这个设计为什么这样子,子类 有有参的构造方法时,为什么父类要有一个无参的构造方法?
private double width;
public double height;
public double Width {
set {
if (0 < value) {
this.width = value;
}
else {
this.width = value;
}
}
get {
return this.width;
}
}
public double Height {
set {
if (0 < value) {
this.height = value;
}
else {
this.height = -1;
}
}
get {
return this.Height;
}
}
public Ractangle(double w, double h) {
if (0 < w && 0 < h) {
this.width = w;
this.height = h;
}
}
}
//这里报错:Page180Ex04.Ractangle' does not contain a constructor that takes 0 arguments
public class ExRactangle:Ractangle {
public ExRactangle(double w, double h,int a) {
if (0 < w && 0 < h) {
base.Width = w;
base.Height = h;
}
}
public double CalcRactangeArea() {
return this.Height * this.Width;
}
}
//也就是说,我不知道这个设计为什么这样子,子类 有有参的构造方法时,为什么父类要有一个无参的构造方法?
解决方案 »
- 【求助】如何实现QQ空间自动写日志功能?
- 一贴解决所有Socket异步处理相关问题
- 100分请教串口数据读取?
- 各位有没有用过NHibernate和Spring.NET的啊?和Java差别大吗?
- DataGridView放在基窗体,再派生新窗体后,无法改到DataGridView的属性??
- visio建模如何表示继承关系?
- 请教关于.NET的DLL转成COM组件的办法
- ListView的一个问题,第1列只能左对齐?
- 继续求~~两样东西~~一样100分~~
- 怎么防止用特定的构造函数实例化相应的对象?
- 删除xml节点
- 在VS2010中,急急急!在第二个td元素之间插入文本内容,在后台如何用C#代码实现
针对上面的代码,因为你的基类定义了一个带参数的构造器,所以编译器不会再为你生成默认的无参构造器,因此在编绎时,为子类构造器调用基类默认构造器会提示不存在。
你也可以在子类构造器中显示调用基类构造器,:base(p1,p2,p3)
请问在c#中怎么调用有参的父类哈...语法貌似不怎么一样...
public int X;
public CBase(int x) {
if (0 < x) {
this.X = x;
}
}
} class CSub: CBase {
public int Y;
public CSub(int x, int y) {
base(x);
this.Y = y;
}
}
你看我这样写不通过...
: base(radius, 0)
{
//还有我想看到这里://Add further instructions here.
}
总之,谢谢3位...