的传承 [英] inheritence

查看:75
本文介绍了的传承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,

我有一个类(基类)继承它的类。

固有的类假设全部基类的特点:

公共属性。我有一个基类获得的参数和

我也将此参数发送给了dervied类约束器。

我收到此错误:

否方法BaseClass()的重载采用0参数。为什么?

谢谢!

***通过Developersdex发送 http://www.developersdex.com ***

解决方案

juli jul写道:

我有一个类(基类)继承它的类。
固有的类假设具有基类的所有功能:
公共属性。我有一个基类得到的参数和
我也把这个参数发送给了dervied类约束器。
我得到了这个错误:
方法BaseClass()没有重载,带有0个参数。为什么?
谢谢!




参见 http://www.pobox.com/~skeet/csharp/constructors.html


总之,构造函数不是没有继承。


如果这对你没有帮助,请发一个简短但完整的例子

来证明这个问题。请参阅
http://www.pobox.com/ ~sibet / csharp / complete.html 我的意思是

那个。


Jon


>简而言之,构造者不会被继承。


Delphites对此非常了解! - 你在想什么Hejlsberg!?


给我们!忘记LINQ; - 我们需要虚拟构造函数,比如

Delphi ..我们在这里很难过.... *试图起床* ... *砰砰*


嗯,我正在进行涉及.NET 2.0和Delphi 7的项目.Delphi

仍然是规则!多态类型是产生差异的原因。

任何人都知道为什么C#走这条路?认真。为什么C#不具有
多态类型?虽然Delphi有原始的RTTI,但.NET得到了反思。我不知道为什么你选择不去变态。


请告诉我......


Happy Coding

- Michael S


ps。

Skeet先生:我希望得到一个好的答案=)


我没有得到它,你怎么能继承构造函数?如果你跳过它的构造函数并直接转到它的基类的构造函数,那么派生的

类将如何被初始化?


就像在这个例子中,由于对象dc2而无法编译。但是如果

它与Delphi一样出色,那么dc2.str和dc2.date将被设置为?b $ b。哪个DerivedClass的构造函数会运行? delphi如何解决这个问题?


公共类BaseClass

{

int n;

public BaseClass(int n)

{

this.n = n;

}

}


公共类DerivedClass:BaseClass

{

string str;

DateTime date;


public DerivedClass(s​​tring s):base(5)

{

this.str = s;

this.date = DateTime.Today;

}


public DerivedClass(DateTime date):base(5)

{

this.str =" hello" ;;

this.date = date;

}


public static void Main()

{

DerivedClass dc1 = new DerivedClass(" hi");

// dc1.n = 5,dc1.str = hi,dc1.date =今天的日期


DerivedClass dc2 = new DerivedClass(10);

/ / dc2.n = 5,dc2.str = ???,dc2.date = ???

}

}


谢谢,Patric

我的C#博客: http://spaces.msn.com/members/ pjsson



Hello,
I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!
*** Sent via Developersdex http://www.developersdex.com ***

解决方案

juli jul wrote:

I have a class (base class) a class which inherites it .
The inhereted class suppose to have all the features of the base class:
the public properties. I have an argument which the base class gets and
I sent this argument to dervied class constractor also.
I get this error:
No overload for method BaseClass() taking 0 arguments. why?
Thank you!



See http://www.pobox.com/~skeet/csharp/constructors.html

In short, constructors aren''t inherited.

If this doesn''t help you, please post a short but complete example
which demonstrates the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon


> In short, constructors aren''t inherited.

Which we Delphites knows all too well! - What where you thinking Hejlsberg!?

Give it to us! Forget about LINQ; - We need virtual constructors like in
Delphi.. We''re dying out here.... *trying to get up* ... *thump*

Well, I''m on a project that involves both .NET 2.0 and Delphi 7. Delphi
still rules! Polymorphic types is what makes the difference.

Anybody knows why C# took this path? Seriously. Why doesn''t C# have
polymorphic types? While Delphi has primitive RTTI, .NET got reflection. I
can''t see why you choose not to go polymporphic.

Tell me please...

Happy Coding
- Michael S

ps.
Mr. Skeet: I''m hoping for a good answer =)


I don''t get it, how can you inherit constructors? How will the derived
class be initialized if you skip its constructor and go directly for
the constructor of its base class?

Like in this example, which don''t compile because of object dc2. But if
it did as apperently with Delphi, what would dc2.str and dc2.date be
set to? Which of DerivedClass''s constructors would run? How does delphi
solve that?

public class BaseClass
{
int n;
public BaseClass (int n)
{
this.n = n;
}
}

public class DerivedClass : BaseClass
{
string str;
DateTime date;

public DerivedClass (string s) : base (5)
{
this.str = s;
this.date = DateTime.Today;
}

public DerivedClass (DateTime date) : base (5)
{
this.str = "hello";
this.date = date;
}

public static void Main()
{
DerivedClass dc1 = new DerivedClass("hi");
// dc1.n = 5, dc1.str = hi, dc1.date = todays date

DerivedClass dc2 = new DerivedClass(10);
// dc2.n = 5, dc2.str = ???, dc2.date = ???
}
}

Thanks, Patric
My C# blog: http://spaces.msn.com/members/pjsson


这篇关于的传承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆