简体   繁体   English

在我的场景中,抽象和受保护之间有什么区别-C#

[英]Whats the difference between abstract and protected in my scenario - C#

What is the difference between a public abstract class with a public constructor, and a public class with a protected constructor. 具有公共构造函数的公共抽象类和具有受保护构造函数的公共类之间有什么区别? We don't have any functions that are abstract in our abstract class, but we want programmers to only be capable of creating objects that extend that class. 我们的抽象类中没有抽象的函数,但我们希望程序员只能创建扩展该类的对象。

Both scenarios compile and work, however I don't understand which would be better to use in what scenario. 两种方案都可以编译和工作,但是我不明白哪种方案更适合使用哪种方案。 I have been brought up to understand that although you cannot instantiate an abstract class directly (only through a non abstract child class), the abstract class should normally contain abstract functions that are required to be implemented by the children of that class. 我从小就明白,虽然您不能直接实例化一个抽象类(只能通过一个非抽象子类),但抽象类通常应包含该类的子类必须实现的抽象函数。

Wouldn't having a protected constructor in a public class signify that instantiation of this class is not possible (this is the only constructor we have). 在公共类中没有受保护的构造函数表示无法实例化此类(这是我们仅有的构造函数)。

MSDN states about using the abstract keyword for classes: Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. MSDN声明了对类使用 abstract关键字: 在类声明中使用abstract修饰符表示一个类仅打算作为其他类的基类。 That is, it is not required that an abstract class contain any abstract members. 也就是说,不需要抽象类包含任何抽象成员。 The abstract modifier is just an explicit way to say that the class shouldn't be instantiated, rather than with technical obstacles such as making constructors publicly invisible. abstract修饰符只是一种明确的方式,它表明不应实例化该类,而不是存在诸如使构造函数公开不可见之类的技术障碍。

Note that the technical obstacle you described even has a caveat: It can still be called: 请注意,您描述的技术障碍甚至有一个警告:它仍然可以称为:

  • It can be invoked from derived classes. 可以从派生类调用它。
  • It can be invoked by using reflection. 可以使用反射来调用它。

Both mean that other developers who are just (ab-?)using your class can do something you did not intend to happen, namely instantiate your class, and both are not possibly when making the class abstract. 两者都意味着仅使用(滥用)类的其他开发人员可以做您不希望发生的事情,即实例化您的类,并且在使类抽象化时都不可能。

Therefore, the answer is that you should mark your classes as abstract . 因此,答案是您应该将类​​标记为abstract

Note that it is adviseable to make the constructor of your abstract class protected nonetheless (to emphasize that the class cannot be instantiated). 请注意,建议仍然使抽象类的构造函数受保护(以强调该类无法实例化)。 Tools such as FxCop will output a warning if an abstract class has a public constructor. 如果抽象类具有公共构造函数,则FxCop之类的工具将输出警告。

This complies with the general rule of making each member just as visible as it really needs to be. 这符合使每个成员真正可见的一般规则。 In an abstract class, constructors will never be invoked from public scope, so public visibility is not required. 在抽象类中,永远不会从公共范围调用构造函数,因此不需要public可见性。 They will only ever be invoked by constructors of derived classes, so protected is the reasonable visiblity for any constructors in an abstract class. 它们只会被派生类的构造函数调用,因此对于抽象类中的任何构造函数而言, protected是合理的可见性。

Hence, also make any constructors of your abstract classes (at most) protected . 因此,还应使(最多)抽象类的任何构造函数protected

I would do a public abstract class with a protected constructor. 我会使用受保护的构造函数来做一个公共抽象类。

Using abstract makes it clear that it's really an abstract class. 使用abstract可以清楚地表明它确实是一个抽象类。 Just a protected constructor isn't as clear. 只是protected构造函数还不清楚。

Nothing other than a sub class can call an abstract constructor; 除了子类外,没有什么可以调用abstract构造函数。 it's kinda meaningless to leave it public . 让它public没有意义的。

A private or protected constructor can still be called by static methods in the declaring class. 私有或受保护的构造函数仍可以由声明类中的静态方法调用。 An abstract class must have a derived class to be instantiated. 抽象类必须具有要实例化的派生类。 For example, the singleton pattern makes use of private constructors called through a public static method/property. 例如,单例模式利用通过公共静态方法/属性调用的私有构造函数。

A public abstract class with a protected or internal constructor conveys how you want calling code to use it. 具有受保护的或内部的构造函数的公共抽象类传达了您希望调用代码使用它的方式。 Using a non-accessible constructor can be actually confusing. 使用不可访问的构造函数实际上可能会造成混淆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中受保护的访问说明符和受保护的内部有什么区别 - What is the difference between access specifier protected and internal protected in C# LRU缓存和内存缓存C#有什么区别 - Whats the difference between LRU Caching and Memory Caching C# c#中的开放句柄和本机句柄有什么区别 - Whats the difference between open and native handle in c# C#hack:接口和抽象类之间的低级别差异 - C# hack: low level difference between interface and abstract class C#:可能没有在抽象类上实现受保护的内部抽象? - C#: Possible to not implement a protected internal abstract on an abstract class? 关闭我的应用程序的这些方法之间有什么区别? - Whats the difference between these methods for closing my application? C#Image.VerticalResolution和Image.Width之间有什么区别 - C# Whats the difference between Image.VerticalResolution and Image.Width 在C#Excel Interop中,新Excel.Workbook()和默认值(Excel.Workbook)之间有什么区别 - In C# Excel Interop whats Difference between new Excel.Workbook() and default(Excel.Workbook) 这三种创建新List的方式有何不同? <string> 在C#? - Whats the difference between these three ways of creating a new List<string> in C#? c#Crystal Reports 13(2010)将参数传递给Report Documents的这些方法之间有什么区别 - c# Crystal Reports 13 (2010) Whats the difference between these methods for passing parameters to Report Documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM