成员变量和成员属性之间的区别? [英] Difference between member variable and member property?

查看:207
本文介绍了成员变量和成员属性之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我在类的顶部声明成员变量,然后还声明一个属性以访问或设置该成员变量,但是我问自己,如果仅要访问该变量,则该属性是否必要?从类内部进行设置,而不是在其他地方进行设置,那么使用属性访问和设置成员变量而不是直接对成员变量本身进行操作的好处是什么?这是一个示例:

There are situations where I declare member variables at the top of my class and then also declare a property to access or set that member variable, but I ask myself if the property is necessary if it variable is only going to be accessed and set from within the class and no where else, so what is the advantage of using a property to access and set a member variable instead of just doing it directly to the member variable itself. Here is an example:

public class Car
{

    int speed; //Is this sufficient enough if Car will only set and get it.

    public Car(int initialSpeed)
    {
        speed = initialSpeed;
    }

    //Is this actually necessary, is it only for setting and getting the member
        //variable or does it add some benefit to it, such as caching and if so,
        //how does caching work with properties.
    public int Speed 
    {
        get{return speed;}
        set{speed = value;}
    }

        //Which is better?
        public void MultiplySpeed(int multiply)
        {
            speed = speed * multiply; //Line 1
            this.Speed = this.Speed * multiply; //Line 2

            //Change speed value many times
            speed = speed + speed + speed;
            speed = speed * speed;
            speed = speed / 3;
            speed = speed - 4;

        }
}

在上面,如果我没有属性Speed来设置和获取可变速度,并且我决定将int speed更改为int spd,那么我将不得不在任何使用它的地方都将speed更改为spd.如果我使用诸如Speed之类的属性来设置和获取速度,则只需要在该属性的get和set中将速度更改为spd,因此在我的MutilplySpeed方法中,类似于this.Speed = this.Speed + this .Speed + this.Speed不会中断.

In the above, if I don't have the property Speed to set and get the variable speed, and I decide to change int speed to int spd, I will have to change speed to spd everywhere it is used, however, if I use a property such as Speed to set and get speed, I will just have to change speed to spd in the get and set of the property, so in my MutilplySpeed method, stuff like above this.Speed = this.Speed + this.Speed + this.Speed will not break.

推荐答案

如果变量是private,我通常不会为其创建属性.无论如何,如果它是在类型之外公开的,出于各种原因,我总是通过属性来公开它:

If the variable is private , I will often not create a property for it. If it is, in any way, exposed outside the type, I always expose it through a property for different reasons:

  • 今天可能没有必要,但是如果以后需要的话,这将是一个重大突破
  • 数据绑定仅适用于属性,不适用于字段(我认为,不是很大的数据绑定用户)
  • 它允许在访问值时插入验证,记录,断点

此外,如果该字段是通过属性公开的,那么即使在类中,我也总是通过该属性来访问它.

Also, if the field is exposed through a property, I always access it through the property, even within the class.

更新
为了响应您更新的代码示例:此处的代码设计需要考虑很多因素.

Update
In response to your updated code samples: there are a number of things to consider around the code design here.

  • 可读性与速度
  • 原子性"
  • 其他副作用

一个典型的建议(我觉得很好)是写清楚,测试性能".这意味着在编写代码时,首先要关注的是在查看代码时是否清楚代码的功能.这通常(但并非总是)比代码的原始速度更重要.在确定获得速度的地方,就可以进行速度优化.与直接阅读该字段相比,访问某个属性要慢一些,但是在大多数情况下,两者之间的差异可以忽略不计(如果可以衡量的话).

One typical piece of advice (that I find very good) is "write for clarity, test for performance". That means that when you write your code, your first concern should be whether it is clear what the code does when looking at it. This is often (but not always) more important than the raw speed of the code. Write speed optimizations when you have established where you gain it. Accessing a property will be a tad slower than reading the field directly, but in most cases, the difference will be negligible (if at all measurable).

原子性可能是个问题.给定您的代码示例,我们有一个字段speed,该字段通过属性Speed公开显示.如果方法MultiplySpeed需要对该值进行多次更新,则在进行计算时,这些中间值将通过Speed属性在不同时间可用.无论您是直接更新字段还是通过属性更新字段,都是如此.在这种情况下,最好先将值放入局部变量,然后将其用于计算,然后在完成后将该变量的值分配回属性.

Atomicity may be an issue. Given your code sample, we have the field speed, that is publicly exposed through the property Speed. If the method MultiplySpeed needs to perform several updates to the value, those intermediate values will be available through the Speed property at different times while the calculation is ongoing. This is true regardless of whether you update the field directly or through the property. In cases like this it is perhaps better to first put the value into a local variable, use that for the calculations and assign the value of that variable back to the property when done.

最后,还有其他副作用.更改Speed的值可能会引发一个事件(例如SpeedChanged).在这种情况下,最好不要等到计算完成后再进行更新.

Lastly, other side effects. It could be that changing the value of Speed should raise an event (such as SpeedChanged). In cases like that, it is also probably a good idea not to make the update until the calculation is done.

我喜欢将属性视为合约,将字段视为实施.任何需要该值的人(除了我这种类型的核心)都应该使用合同.只有在有充分的理由绕过合同的情况下,才应依靠实施.

I like to think about the property as a contract and the field as implementation. Anybody (except for the core of my type) that needs the value should use the contract. Relying on the implementation should be done only if there are good reasons to bypass the contract.

是的,如果您将对字段的访问封装在属性中,则自然地更改字段名称将需要较少的更新(也许字段名称也变得不太重要).

And yes, if you encapsulate the access to the field in the property, naturally changing the name of the field will require less updates (and perhaps also the name of the field becomes less important).

我希望这是有道理的,而且不要太离题;)

I hope that makes sense, and is not too much off topic ;)

这篇关于成员变量和成员属性之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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