简体   繁体   English

c#使用基类的setter或getter

[英]c# using setters or getters from base class

Is it recommended to set member variables of a base class to protected, so that subclasses can access these variables? 是否建议将基类的成员变量设置为protected,以便子类可以访问这些变量? Or is it more recommended to set the member variables to private and let the subclasses get or set the varible by getters and setters? 或者是否更建议将成员变量设置为private并让子类通过getter和setter获取或设置变量?

And if it is recommended to use the getters and setters method, when are protected variables used? 如果建议使用getter和setter方法,何时使用受保护的变量?

This is very similar to this question , about whether to access information within the same class via properties or direct access. 此问题非常相似 ,关于是否通过属性或直接访问访问同一类中的信息。 It's probably worth reading all those answers too. 这也许值得阅读所有这些答案。

Personally, I don't like any fields to be non-private with the occasional exception of static readonly fields with immutable values (whether const or not). 就个人而言,我不喜欢任何字段是非私有的,偶尔会出现具有不可变值的静态只读字段(无论是否为const)。 To me, properties just give a better degree of encapsulation. 对我而言,属性只是提供更好的封装程度。 How data is stored is an implementation decision, not an API decision (unlike properties). 数据的存储方式是实现决策,而不是API决策(与属性不同)。 Why should class Foo deriving from class Bar care about the implementation of class Bar? 为什么类Foo派生于Bar类关心类Bar的实现

In short, I'd always go for properties, and I don't use protected variables for anything other than throwaway test code. 简而言之,我总是寻找属性,除了一次性测试代码之外我不会使用受保护的变量。

With automatically implemented properties in C# 3.0, it's easier than ever before to turn fields into properties. 通过在C#3.0中自动实现的属性,将字段转换为属性比以往更容易。 There's precious little reason not to do it. 没有理由去做。

Classes in other assemblies can derive from your unsealed classes and can access protected fields. 其他程序集中的类可以从未密封的类派生,并可以访问受保护的字段。 If you one day decide to make those fields into properties, those classes in other assemblies will need to be recompiled to work with the new version of your assembly. 如果有一天您决定将这些字段设置为属性,则需要重新编译其他程序集中的那些类以使用新版本的程序集。 That's called "breaking binary compatibility", and is perhaps the one solid reason why you shouldn't ever expose fields outside of an assembly. 这被称为“打破二进制兼容性”,也许是你不应该在程序集之外公开字段的一个坚实原因。

This is a trade-off here. 这是一个权衡。 Setters and getters are somewhat slower than accessing fields directly, so if you are doing heavy maths and read/write these fields a lot in your subclasses, you should go for accessing the fields directly. Setter和getter比直接访问字段慢一些,所以如果你在子类中进行大量的数学运算和读/写这些字段,你应该直接访问字段。 But this is more like an exception. 但这更像是一个例外。

Normally, you should mark them as private and go for getters/setters. 通常情况下,您应该将它们标记为私有,然后选择getter / setter。

So my answer is: direct access for heavily used fields, getters/setters otherwise. 所以我的答案是:直接访问频繁使用的字段,否则为getter / setter。 Use common sense. 使用常识。

EDIT: I did some profiling and apparently even in Release mode, there can be up the 20% speed difference between fields and properties. 编辑:我做了一些分析,显然甚至在发布模式下,字段和属性之间的速度差异可达20%。 See my test case here: http://pastebin.com/m5a4d1597 请在此处查看我的测试用例: http//pastebin.com/m5a4d1597

I have to agree with Jon. 我必须同意乔恩。

But, I use protected variable for "top most" inheritance class sometime in some condition. 但是,在某些情况下,我有时会将保护变量用于“最顶层”的继承类。 Example, if you have an object that is readonly and you cannot set it back BUT that you can use it in a child class, I do not see why I should have a protected Get to have access to that variable. 例如,如果你有一个readonly对象并且你不能将它设置回来但是你可以在子类中使用它,我不明白为什么我应该有一个受保护的Get来访问该变量。 A simple protected variable do the same encapsulation because you cannot set this variable and you can access this variable only from the child class. 一个简单的受保护变量执行相同的封装,因为您无法设置此变量,并且只能从子类访问此变量。

But set/get is the way to do for other situation. 但是set / get是其他情况的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM