C#抽象类中公共构造函数的作用是什么? [英] Whats the utility of public constructors in abstract classes in C#?

查看:25
本文介绍了C#抽象类中公共构造函数的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果抽象类中的公共构造函数只能由它们的派生类调用,那么它应该在功能上等同于受保护的构造函数.对吗?

If a public constructor in an abstract class can only be called by their derived classes it should be functionally equivalent to a protected constructor. Right?

在抽象类中声明公共构造函数而不是受保护的构造函数有什么区别吗?你会用它做什么?为什么编译器不抱怨?

Is there any difference in declaring a public constructor, instead of a protected one, in an abstract class? What would you use it for? Why the compiler doesn't complaint?

推荐答案

完全正确.你应该支持受保护的构造函数.

Absolutely correct. You should favor the protected constructor.

不,编译器不会抱怨,但像 FxCop(& 代码分析)这样的工具会.我相信你可以用抽象类上的公共构造函数来做一些奇怪的反射技巧,但从你只是为其他编写子类的开发人员提供基类功能的角度来看,坚持使用受保护的构造函数.

no the compiler doesn't complain, but tools like FxCop (& Code Analysis) do. I believe there are some weird reflection tricks you can do with public constructors on abstract classes, but from a standpoint where you are merely providing base class functionality to other developers writing subclasses, stick with the protected constructor.

这篇关于C#抽象类中公共构造函数的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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