C#构造者惹恼了我! [英] C# constructors annoy me!

查看:44
本文介绍了C#构造者惹恼了我!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这两个班级


公共班级考试


{


public readonly string name;


public Test(字符串名称)


{


this.Name =姓名;


}


}


公共类测试2:测试
< br $>
{


}


让我烦恼的是,尝试执行以下代码给了我一个
错误,Test2没有一个构造函数需要1个参数。


new Test2(Hello);


很明显我想使用继承的构造函数,为什么

不会用C#使用它?

Look at these two classes

public class Test

{

public readonly string Name;

public Test(string name)

{

this.Name = name;

}

}

public class Test2 : Test

{

}

What annoys me is that trying to execute the following code gives me an
error that Test2 doesn''t have a constructor that takes 1 argument.

new Test2("Hello");

It''s pretty obvious that I want to use the inherited constructor, so why
wont C# use it?

推荐答案

因为构造函数不被继承。


Peter Morris [Droopy眼睛软件]写道:
Because constructors aren''t inherited.

Peter Morris [Droopy eyes software] wrote:
看看这两个类
公共课测试

公共只读字符串Na我;

公共测试(字符串名称)

{

this.Name = name;

}

}

公共课Test2:测试

{

}

让我烦恼的是试图执行以下代码给我一个错误,Test2没有一个构造函数需要1个参数。

新的Test2(你好);

很明显我想使用继承的构造函数,为什么不用C#来使用它?
Look at these two classes

public class Test

{

public readonly string Name;

public Test(string name)

{

this.Name = name;

}

}

public class Test2 : Test

{

}

What annoys me is that trying to execute the following code gives me an
error that Test2 doesn''t have a constructor that takes 1 argument.

new Test2("Hello");

It''s pretty obvious that I want to use the inherited constructor, so why
wont C# use it?



将类Test2更改为


公共类Test2:测试

{

public Test2(stirng name)

{

base(name);

}

}

change class Test2 to

public class Test2 : Test
{
public Test2(stirng name)
{
base(name);
}
}


" Peter Morris [Droopy eyes software]" < pe ** @droopyeyes.no.com.spam>

写道:
"Peter Morris [Droopy eyes software]" <pe**@droopyeyes.no.com.spam>
wrote:
很明显,我想使用继承的构造函数那么为什么不用C#来使用它?
It''s pretty obvious that I want to use the inherited constructor, so why
wont C# use it?




构造函数不是方法,所以它们不需要符合LSP:

Barbara Liskov的替换原则。


在Delphi中,构造函数是元类的有效方法,

作为静态函数分配工厂对象。因此,继承构造函数是有意义的,因为它们确实是方法。


Liskov替换原则意味着如果你有一个实例

的C类来自B类,你应该可以在任何使用B实例的地方使用一个

的C实例。由于构造函数

不是方法,他们不需要这种可替代性支持。


后代类可以隐藏继承的构造函数的事实可以实际上是
被视为一种功能。子类正是这样的:子类,

比它们的基类超类更具体和更不通用。因为它们可能需要更多的信息来构建,所以它们需要更多的信息来构建,因此需要一个能够获取更多信息的构造函数。


如果继承了基类的构造函数,那么这将会打破后代类的假设。


另外,你可以看看如何东西版。如果你已经从你最喜欢的组件作者的B类中写下了一个

子类C,那么你可能对你已经完成的构造函数有
的要求通过

覆盖基类的构造函数。如果,在下一个版本中,

B会增加更多构造函数,该怎么办?它可以打破你班级的假设

C,因为会有一种新方法来构建它 - 你没有b $ b有机会验证自己。


- 巴里



Constructors aren''t methods, so they don''t need to conform to the LSP:
Barbara Liskov''s Substitution Principle.

In Delp constructors are effectively methods of the metaclass, which
acts as a statically allocated factory object. It therefore makes sense
to inherit constructors, because they really are methods.

The Liskov Substitution Principle means that if you''ve got an instance
of a class C descended from a class B, you should be able to use an
instance of C wherever you use an instance of B. Since constructors
aren''t methods, they don''t need this substitutability support.

The fact that a descendant class can hide inherited constructors can
actually be seen as a feature. Subclasses are exactly that: subclasses,
more specific and less general than their base superclass. It stands to
reason that they may require more information to construct, and thus
require a constructor which takes more information.

If the constructors from the base class were inherited, then this would
break the descendant class''s assumptions.

Alternatively, you can look at how things version. If you''ve written a
subclass C descended from your favourite component author''s B class, you
may have requirements for your constructors that you''ve fulfilled by
overriding the base class''s constructors. What if, in the next version,
B adds more constructors? It could break the assumptions of your class
C, because there would be a new way to construct it - that you didn''t
have the chance to validate yourself.

-- Barry


这篇关于C#构造者惹恼了我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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