如果您使用 getter 和 setter,您应该如何命名私有成员变量?

发布于 2024-07-05 23:11:25 字数 247 浏览 8 评论 0原文

As kind of a follow up to this question about prefixes, I agree with most people on the thread that prefixes are bad. But what about if you are using getters and setters? Then you need to differeniate the publicly accessible getter name from the privately stored variable. I normally just use an underscore, but is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

荒路情人 2024-07-12 23:11:26

喜欢写“this.x = x”。 我很清楚。 另外,当使用 Eclipse 时,您可以让它以这种方式自动生成 getter/setter。

I like writing "this.x = x". It's very clear to me. Plus, when using Eclipse, you can have it automatically generate your getters/setters this way.

硬不硬你别怂 2024-07-12 23:11:25

这是一个完全主观的问题。 没有“更好”的方法。

一种方法是:

private int _x;
public get x():int { return _x; }
public set x(int val):void { _x = val; }

另一种方法是:

private int x;
public get X():int { return x; }
public set X(int val):void { x = val; }

都不是正确答案。 每种风格都有优点和缺点。 选择您最喜欢的一种并持续应用。

This is a completely subjective question. There is no "better" way.

One way is:

private int _x;
public get x():int { return _x; }
public set x(int val):void { _x = val; }

Another is:

private int x;
public get X():int { return x; }
public set X(int val):void { x = val; }

Neither is the right answer. Each has style advantages and disadvantages. Pick the one you like best and apply it consistently.

娇柔作态 2024-07-12 23:11:25

有多少程序员就有多少种不同的方法,但一些更流行的方法包括(对于属性 Foo):

  • mFoo
  • m_foo
  • _foo
  • foo

There are almost as many different ways of doing this as there are programmers doing this, but some of the more popular ways include (for a property Foo):

  • mFoo
  • m_foo
  • _foo
  • foo
笑脸一如从前 2024-07-12 23:11:25

在区分大小写的语言中,我只使用:

private int myValue;

public int MyValue
{
    get { return myValue; }
}

否则我会使用下划线

Private _myValue As Integer

Public ReadOnly Property MyValue As Integer
    Get
        Return _myValue
    End Get
End Property

In a case sensitive language I just use:

private int myValue;

public int MyValue
{
    get { return myValue; }
}

Otherwise I would use an underscore

Private _myValue As Integer

Public ReadOnly Property MyValue As Integer
    Get
        Return _myValue
    End Get
End Property
无敌元气妹 2024-07-12 23:11:25

在java中有this.foo,在python中有self.foo,其他语言也有类似的东西,所以当我已经可以使用语言构造时,我认为不需要以特殊的方式命名某些东西。 在相同的上下文中,好的 IDE 和编辑器可以理解成员变量并给它们一个特殊的突出显示,因此您可以真正看到它而无需使用特殊名称。

In java there is this.foo in python there is self.foo and other languages have similar things, so I don't see a need for naming something in a special way, when I can already use a language construct. In the same context good IDEs and editors understand member variables and give them a special highlight, so you can really see it w/o using special names.

扬花落满肩 2024-07-12 23:11:25

正如其他人提到的,我喜欢在字段前加上下划线。

private int _x;

我认为这超出了个人偏好(正如大卫·阿诺在这篇文章中所说)。 我认为这样做有一些真正客观的原因:

  1. 这意味着您不必为赋值编写“this.x = x”(尤其是在 setter 和构造函数中)。
  2. 它将您的字段与局部变量/参数区分开来。 这样做很重要:字段比本地字段更难处理,因为它们的范围更广/生命周期更长。 添加额外的字符对于编码员来说是一个心理警告信号。
  3. 在某些 IDE 中,下划线将导致自动完成功能将字段排序到建议列表的顶部。 这样可以更轻松地在一个块中查看该类的所有字段。 这反过来会有所帮助; 在大型类中,您可能无法在与您正在处理的代码相同的屏幕上看到字段(通常在类的顶部定义)。 将它们排序到顶部可以提供方便的参考。

(这些约定适用于 Java,但其他语言也存在类似的约定)

这些东西看起来很小,但它们的流行确实让我在编码时的生活变得更轻松。

I like prefixing fields with an underscore, as others have mentioned.

private int _x;

I think this goes beyond straight personal preference though (as David Arno said in this thread). I think there's some real objective reasons for doing this:

  1. It means you avoid having to write "this.x = x" for assignments (especially in setters and constructors).
  2. It distinguishes your fields from your local variables/arguments. It's important to do this: fields are trickier to handle than locals, as their scope is wider / lifetime is longer. Adding in the extra character is a bit of a mental warning sign for coders.
  3. In some IDEs, the underscore will cause the auto-complete to sort the fields to the top of the suggestion list. This makes it easier to see all the fields for the class in one block. This in turn can be helpful; on big classes, you may not be able to see the fields (usually defined at the top of the class) on the same screen as the code you're working on. Sorting them to the top gives a handy reference.

(These conventions are for Java, but similar ones exist for other languages)

These things seems small but their prevalence definitely makes my life easier when I'm coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文