抽象类设计:为什么不界定公共constructores? [英] Abstract Class Design: Why not define public constructores?

查看:131
本文介绍了抽象类设计:为什么不界定公共constructores?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看这里(抽象类设计): http://msdn.microsoft。 COM / EN-US /库/ ms229047.aspx

Look here (Abstract Class Design): http://msdn.microsoft.com/en-us/library/ms229047.aspx

它说:

(1)不要在抽象类定义的公共或受保护的内部(在Visual Basic中受保护的朋友)构造函数。

在C#中,我们是不是能够实例化一个抽象类。所以,它还是重要界定公共构造在C#中抽象类? ?还是不是抽象类,因为语义写作公共构造

In C#, we are not able to instantiate an abstract class. So, does it still matter to define public constructors for abstract classes in C# ? Or not writing public constructors for abstract classes because of semantic meaning?

它还说:

(2)不要定义一个受保护的或抽象类的内部构造。

定义内部构造?在(1),它告诉我们,不限定内部保护构造是因为的构造公共或受保护的内部的可视性是可实例化类型。不界定内部构造抽象类突破(1)?

Define internal constructors ?? In (1), it tells us that not defining internal protected constructors is because that "Constructors with public or protected internal visibility are for types that can be instantiated". Doesn't defining internal constructors for abstract classes break the rules in (1) ?

由于规则提前。 :)

推荐答案

让我们来看看每个案件


  • 保护 - 最明显的例子 - 所有子类可以调用构造函数,不论他们居住在哪个组件(只要抽象基类的本身的可见它们)。

  • protected - The most obvious case - all subclasses can call the constructor, irrespective of which assembly they reside in (as long as the the abstract base-class itself is visible to them).

内部 - 用于需要抽象类型是公开的可见的,但没有公开的继承的。在这种情况下,你会想要让所有的非私有构造内部的。只有的相同的组件设置为抽象基类中的子类将能够调用构造函数 - 有效的,只有他们将能够继承。另一个用例是如果你想要一个特别的构造函数,只能是同样装配的子类是可见的。

internal - Useful when you want the abstract type to be publicly visible, but not publicly inheritable. In this case, you would want to make all of the non-private constructors internal. Only subclasses within the same assembly as the abstract base-class would be able to call the constructors - effectively, only they would be able to inherit. Another use-case would be if you wanted a 'special' constructor that should only be visible to same-assembly subclasses.

私人 - 主要用于用于由当使用构造函数链抽象类的其他构造有针对性的肮脏工作的构造。唯一的其他用途,当的所有的构造函数是私有的,是只允许通过的嵌套的类,有机会获得包含类的私有成员。

private - Used mainly for 'dirty-work' constructors that are targeted by other constructors of the abstract class when it uses constructor-chaining. The only other use, when all the constructors are private, is to only allow subclassing by nested classes, which do have access to private members of the containing-type.


  • 公开 - 没有用的,行为的相同保护 。只有子类可以反正调用构造函数,由于基类是抽象的。

  • public - Not useful, behaves identically to protected. Only subclasses can call the constructor anyway, since the base-class is abstract.

受保护的内部 - 这也是从没有什么不同保护。在受保护的内部访问级别手段加以保护或内部,而不是保护和内部。但是,内部这里修改也是没有用处的 - 它不的阻止的从调用构造函数驻留在组装外的子类(假设抽象基地 - 类是公共的),因为他们可以依靠保护进入,也不允许同一装配类型是的的从调用它的子类(类型,是抽象的)

protected internal - This too is no different from protected. The protected internal accessibility level means protected OR internal, not protected AND internal. However, the internal modifier here serves no purpose - it doesn't prevent subclasses residing outside the assembly from calling the constructor (assuming the abstract base-class is public) since they can rely on protected access, nor does it allow same-assembly types that are not subclasses from calling it (the type is abstract).

这里的关键点是,每一个非 - 私人构造在抽象类是已经的充其量保护。香草内部 -modifier加强对谁可以调用构造函数的限制。 公共受保护的内部,因为它们的显示的走弱限制不会完成任何事情,但不这样做真的很成功。

The key point here is that every non-private constructor in an abstract class is already at best protected. The vanilla internal-modifier strengthens restrictions on who can call the constructor. public and protected internal don't accomplish anything because they appear to weaken restrictions, but don't really succeed in doing so.

这篇关于抽象类设计:为什么不界定公共constructores?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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