你们怎么命名构造方法的参数? [英] How do you guys name the parameters of constructing methods?

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

问题描述

例如,我有一个具有内部变量年份,月份的类日期

和日期。

在Java中我会写:


class Date {

int year;

int month;

int day;

Date( int year,int month,int day){

this.year = year;

this.month = month;

this.day;

}

}


也就是说,只需将每个参数命名为它并使用它。*

在方法体内区分类变量和

参数。


但是,在C ++中我似乎无法做到这一点。在这个特殊的

例子中,我可以写一下:


Date :: Date(int y,int m,int d):year(y),月(m),日(d){

}


然而,字母y,m等。或d或d。不是那么翔实。因此我

不喜欢这种命名风格。

还有更好的方法吗?

你们会用什么?替换y,m和m。和d在这种情况下?

For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}

That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

However, in C++ it seems I cannot do this. In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don''t really like this naming style.
Is there a better way to do this?
What would you guys use to replace "y", "m" and "d" in this case?

推荐答案

12月18日下午4:39,xz< zhang.xi ... @ gmail。 comwrote:
On Dec 18, 4:39 pm, xz <zhang.xi...@gmail.comwrote:

例如,我有一个内部变量年份,月份

和日期的类日期。

在Java中我会写:


class Date {

int year;

int month;

int day;

日期(int year,int month,int day){

this.year = year;

this.month =月;

this.day;

}


}


那个是,简单地将每个参数命名为它是什么并使用它。*

在方法体内区分类变量和

参数。


但是,在C ++中我似乎无法做到这一点。在这个特殊的

例子中,我可以写一下:


Date :: Date(int y,int m,int d):year(y),月(m),日(d){


}


然而,字母y,m等。或d或d。不是那么翔实。因此我

不喜欢这种命名风格。

还有更好的方法吗?

你们会用什么?替换y,m和m。和d在这种情况下?
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}

}

That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.

However, in C++ it seems I cannot do this. In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {

}

However, the letters "y", "m" or "d" are not that informative. Thus I
don''t really like this naming style.
Is there a better way to do this?
What would you guys use to replace "y", "m" and "d" in this case?



没有正确的方法,这是一个风格问题。


类日期

{

int m_year; //成员

int m_month;

int m_day;

public:

Date():m_year(2000 ),m_month(1),m_day(1)

{}

日期(int year,int month,int day):m_year(year),

m_month(月),

m_day(天)

{}

};


或使用年份和年份,但优选不是_year

There is no right way, its a question of style.

class Date
{
int m_year; // members
int m_month;
int m_day;
public:
Date() : m_year(2000), m_month(1), m_day(1)
{ }
Date(int year, int month, int day): m_year(year),
m_month(month),
m_day(day)
{ }
};

or use year_ and year but prefereably not _year


xz写道:
xz wrote:

例如,我有一个类日期,内部变量年,月

和日。

在Java中我会写:


class Date {

int year;

int month;

int day;

Date(int year, int month,int day){

this.year = year;

this.month = month;

this.day;

}

}
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:

class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}



在C ++中你会做什么


上课日期{

int year;

int month;

int day ;

public://你可能意味着

日期(int year,int month,int day)

:年(年),月(月),日(日){}

};

In C++ you''d do

class Date {
int year;
int month;
int day;
public: // you probably meant that
Date(int year, int month, int day)
: year(year), month(month), day(day) {}
};


也就是说,只需将每个参数命名为它是什么并使用这个。*

在方法体内区分类变量和

参数。
That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the
parameters.



与C ++相同。你需要''this->''来区分。

Same with C++. You need ''this->'' to differenciate.


但是,在C ++中我似乎无法做到这一点。
However, in C++ it seems I cannot do this.



是这样的吗?发布完整的可编辑代码和错误消息[s]

你得到。

Is that so? Post the complete compilable code and the error message[s]
you get.


在这个特殊的

例如,我可以写:


Date :: Date(int y,int m,int d):year(y),month(m),day(d){< br $> b $ b}


但是,字母y,m和或d或d。不是那么翔实。因此我

不喜欢这种命名风格。

有更好的方法吗?
In this particular
example, probably I can write:

Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}

However, the letters "y", "m" or "d" are not that informative. Thus I
don''t really like this naming style.
Is there a better way to do this?



见上文。

See above.


你们用什么代替y,m ;和d在这种情况下?
What would you guys use to replace "y", "m" and "d" in this case?



我已经看到(和使用过)遵循参数名称的约定

下划线:


日期(int year_,int month_,int day_)

:年(年),月(月_),日(day_){}


或(我知道看到它时会有些畏缩)只需给你的成员命名

带前缀(如''m_''),参数和局部变量 - 没有

prefices。很容易辨别什么是会员,什么不是。

V

-

请删除资金''当用电子邮件回复时,我会回答这个问题。我不回复最热门的回复,请不要问

I''ve seen (and used) the convention to follow the argument names with
an underscore:

Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like ''m_''), arguments and local variables - without
prefices. It''s easy to discern what is a member and what isn''t.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 12月18日下午3:49,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Dec 18, 3:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

xz写道:
xz wrote:

例如,我有一个内部变量年,月的类日期

和日。

在Java中我会写:
For example, I have a Class Date with internal variable year, month
and day.
In Java I would write:


class Date {

int year;

int month;

int int;

日期(int year,int month,int day){

this.year = year;

this.month = month;

this .day;

}

}
class Date{
int year;
int month;
int day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day;
}
}



在C ++中你会做什么


class Date {

int year;

int month;

int int;

public://你可能意味着

日期(int year,int month,int day)

:年(年),月(月),日(日) {}

};


In C++ you''d do

class Date {
int year;
int month;
int day;
public: // you probably meant that
Date(int year, int month, int day)
: year(year), month(month), day(day) {}
};


也就是说,只需将每个参数命名为它,并使用它。*

在方法体内区分类变量和
That is, simply name every parameter as what it is and use this.*
within the method body to differentiate the class variables and the


参数。
parameters.



与C ++相同。你需要''this->''来区分。


Same with C++. You need ''this->'' to differenciate.



现在我知道出了什么问题。 "这"在C ++中是指针而不是

引用,所以你必须使用this->而不是这个。

谢谢!

Now I know what was wrong. "this" in C++ is pointer instead of
reference so that you have to use "this->" instead of "this."
thanks!


但是,在C ++中似乎我不能做这个。
However, in C++ it seems I cannot do this.



是这样吗?发布完整的可编辑代码和错误消息[s]

你得到。


Is that so? Post the complete compilable code and the error message[s]
you get.


在这个特殊的

例如,我可以写:
In this particular
example, probably I can write:


Date :: Date(int y,int m,int d):year(y),month(m) ),(d){

}
Date::Date(int y, int m, int d): year(y), month(m), day(d) {
}


但是,字母y,m和m。或d或d。不是那么翔实。因此我

不喜欢这种命名风格。

有更好的方法吗?
However, the letters "y", "m" or "d" are not that informative. Thus I
don''t really like this naming style.
Is there a better way to do this?



见上文。


See above.


你们用什么代替y,m ;和d在这种情况下?
What would you guys use to replace "y", "m" and "d" in this case?



我已经看到(和使用过)遵循参数名称的约定

下划线:


日期(int year_,int month_,int day_)

:年(年),月(月_),日(day_){}


或(我知道看到它时会有些畏缩)只需给你的成员命名

带前缀(如''m_''),参数和局部变量 - 没有

prefices。很容易辨别什么是会员,什么不是。


V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问


I''ve seen (and used) the convention to follow the argument names with
an underscore:

Date(int year_, int month_, int day_)
: year(year_), month(month_), day(day_) {}

Or (I know some cringe at a mere sight of it) just name your members
with a prefix (like ''m_''), arguments and local variables - without
prefices. It''s easy to discern what is a member and what isn''t.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于你们怎么命名构造方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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