OO:是否应该通过类内部的属性访问私有变量? [英] OO: Should you access your private variables through properties inside your class?

查看:78
本文介绍了OO:是否应该通过类内部的属性访问私有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是好的做法:

I was wondering, what is good practice:

 private int value;

 public int Value { get { return this.value; } }

 private int DoSomething()
 {
      return this.Value + 1;
      //OR
      return this.value + 1;
 }

因此,问题在于您应如何对待类变量.您应该通过属性访问还是直接访问它们?

So, the question is about how you should treat your class variables. Should you access them through your properties or just direct?

推荐答案

在这种情况下,它没什么大不了的,但是在您使用延迟加载的变量的情况下,这很重要,并且您可以通过财产.例如:

In this case, it doesn't matter so much, but in cases where you're using lazy loaded variables it would matter and you'd access through your property. For instance:

private Myobject _value;

public Myobject Value
{
    get
    {
        if (_value == null) _value = new MyObject();
        return _value;
    }
}

private MyObject DoSomething();
{
    //If you access _value here, it might be null...
    //so you should access it through the property:
    return Value;
}

如果您的方法直接调用该字段而失败,则您可以对其进行正确处理,也可以通过更清洁的方法(通过您的属性)对其进行访问.

In a case where your method would fail by calling the field directly, you either handle it properly, or you access it by the cleaner method - via your property.

这一切都取决于应用程序的体系结构,为此您必须提出一个问题:从维护的角度来看,什么才是最有意义的?

It all comes down to the architecture of your application and for that you have to ask the question: What makes the most sense from a maintenance perspective?

我不得不说,如果该字段已正确初始化,并且不会引起您直接访问它,然后直接访问它的麻烦.如果它造成了额外的工作(从而造成了额外的维护),请引用该属性.

I would have to say that if the field is initialized properly and it doesn't cause you a headache to access it directly, then access it directly. If it causes extra work (and thus extra maintenance), then reference the property.

与其他任何事物一样,权衡利弊.使用您自己的常识-标准战争可笑.除非他们为您提供您尚未想到的论据,否则请不要为与他们抗争而浪费时间.如果您有合理的理由来选择所选择的任何一条路径,那么该路径就是正确的-这取决于设计师的特权.

Like with anything else, weigh up the pros and cons. Use your own common sense - standards wars are a ridiculous distraction. Unless they provide you with arguments you haven't already thought of yourself, don't waste your breath fighting with them. If you have a valid reason to choose whichever path you choose, then that path is the right one - it comes down to the designer's prerogative.

我对利弊的看法是:

与财产共处

  • 我可以更改返回该值的方式的实现,而无需重构整个类.
  • 我可以延迟加载该值,这意味着如果我从不访问它,就永远不会浪费资源.
  • 我可以在一个地方隐藏如何返回值的所有实现细节,而不必在我的代码中全部处理.

随行随行:

  • 我没有潜在的性能开销,不必每次访问该值时都必须遍历属性代码.
  • 我需要确保在每次调用时都正确初始化了该值,或者处理了未正确初始化的情况.
  • 即使我的属性可能仅提供指向我的值的只读接口,我还是会影响该值.
  • I don't have the potential performance overhead of having to step through the property code each time I access the value.
  • I need to make sure the value is initialized properly on every call, or handle cases where it is not.
  • I can affect the value even though my property may only provide a read-only interface to my value.

所以我想的方法是使用属性,除非我需要直接将值写入值,在这种情况下,我会选择该字段-因为我的属性是只读的,因此无法写入.

So I guess my approach would be to use the property unless I needed to write to the value directly, in which case, I'd go with the field - as my property is read-only and thus can't be written to.

不过那只是我-您的属性可以被读取/写入,并且您可以从设计的角度决定直接访问该字段是可以的-而且也可以.

That's just me though - your property may be read/write and you may decide from a design standpoint that accessing the field directly is okay - and that is okay too.

诀窍是 总是出于某种原因做事,不要仅仅因为而盲目做事.

The trick is always do things for a reason, don't do things blindly just because.

这篇关于OO:是否应该通过类内部的属性访问私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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