Inheritance C#-构造函数和继承
我有两个类声明如下:Inheritance C#-构造函数和继承,inheritance,c#-3.0,constructor,Inheritance,C# 3.0,Constructor,我有两个类声明如下: class Object1 { protected ulong guid; protected uint type; public Object1(ulong Guid, uint Type) { this.guid = Guid; this.type = Type } // other details omitted } class Object2 : Object1 { // d
class Object1
{
protected ulong guid;
protected uint type;
public Object1(ulong Guid, uint Type)
{
this.guid = Guid;
this.type = Type
}
// other details omitted
}
class Object2 : Object1
{
// details omitted
}
Object1 MyObject1 = new Object1(123456789, 555);
Object2 MyObject2 = new Object2(987654321, 111);
在客户端代码中,我希望能够创建每个对象的实例,如下所示:
class Object1
{
protected ulong guid;
protected uint type;
public Object1(ulong Guid, uint Type)
{
this.guid = Guid;
this.type = Type
}
// other details omitted
}
class Object2 : Object1
{
// details omitted
}
Object1 MyObject1 = new Object1(123456789, 555);
Object2 MyObject2 = new Object2(987654321, 111);
如何让Object2像那样使用Object1的构造函数?
谢谢。您必须为Object2提供具有相同签名的构造函数,然后调用基类的构造函数:
class Object2 : Object1
{
public Object2(ulong Guid, uint Type) : base(Guid, Type) {}
}
为Object2提供如下构造函数:-
public Object2(ulong Guid, uint Type): base(Guid, Type)
爆炸。。。您必须以比光速更快的速度键入,才能首先在此网站上获得答案!;-)不,你只需要在键入答案时停止计时。简单。只有两分钟的差距——也就是120秒。有人能解释一下为什么必须这样做吗?我很感激你的回答!