如何命名构造函数参数和成员变量? [英] How do you name constructor argument and member variables?

查看:139
本文介绍了如何命名构造函数参数和成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命名类的内部变量时,我不使用前缀(我知道有些人可以,但是我并没有开始你为什么……"的辩论).我只是喜欢那样.问题是有时在构造函数中传递相同的参数,而最终我却对如何命名它们感到困惑.例如:

I don't use prefix while naming internal variables of a class (I know some do but I am not starting "why do you..." debate). I just prefer it that way. The problem is that sometimes same parameters are passed in contructor and I end up being confused how to name them. For example:


public class SampleClass
{
    private int classId;
    private string className;

    public SampleClass (int XclassIdX, string XclassNameX) {
        classId = XclassIdX;
        className = XclassNameX;
    }
}

如何命名XclassIdX和XclassNameX?

How to name XclassIdX and XclassNameX?

可能要做的一件事是:


public class SampleClass
{
    private int classId;
    private string className;

    public SampleClass (int classId, string className) {
        this.classId = classId;
        this.className = className;
    }
}

只是不确定这是一个好主意还是还有其他更精致的方法?

Just not sure whether this is a good idea or there are other more elgant ways?

推荐答案

我认为您正在描述的解决方案是正确的,在该解决方案中,构造函数参数的名称相同,并且您在类成员之前添加了this作为前缀.很清楚,很简洁,您的意思也没有任何困惑.

I think the solution you're describing, where the constructor parameters are identically named and you prefix the class members with this, is just fine. It's clear, it's concise, and there is no confusion about what you meant.

这篇关于如何命名构造函数参数和成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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