从私有变量或类功能的实际使用物业的价值? [英] Using the value from the private variable or from the actual property in class functions?

查看:78
本文介绍了从私有变量或类功能的实际使用物业的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从一个函数的类中引用类的属性你使用从实际财产或私有变量值的价值呢?

哪种方法是最好的?为什么呢?

 公共类

  私人m_Foo为双

  公共财产美孚()为双
    得到
      返回m_Foo
    最终得到
    集(BYVAL值作为双)
      m_Foo =价值
    最终设置
  高端物业

  公共函数bar()为双
    昏暗x作为双= 5 * m_Foo
    昏暗y为双= 3 *富
  端功能

末级
 

解决方案

就个人而言,我尝试使用get / set访问尽可能避免奇怪我自己,当我改变自己的逻辑和突然的地方,我进入的是私人领域的唐T正常工作。

When referencing class properties from a function within the class do you use the value from the actual property or the private variable value?

Which way is best? Why?

public class

  private m_Foo as double

  public property Foo() as double
    get
      return m_Foo
    end get
    set(byval value as double)
      m_Foo = value
    end set
  end property

  public function bar() as double
    Dim x as double = 5 * m_Foo
    Dim y as double = 3 * Foo
  end function

end class

解决方案

Personally, I try to use the get/set accessor whenever possible, to avoid surprising myself when I change their logic and suddenly places where I access the private field don't work as expected.

这篇关于从私有变量或类功能的实际使用物业的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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