c# 这是我重写方法的方式吗?
// Loads a users info
public void loadUserInfo()
{
CrystalTech.tblUsersDataTable dsCommon = new CrystalTech.tblUsersDataTable();
using (tblUsersTableAdapter userAdapter = new tblUsersTableAdapter())
{
userAdapter.FillBy(dsCommon, this.ID);
}
this.username = dsCommon[0].userName;
this.company.ID = dsCommon[0].clientID;
this.company.name = dsCommon[0].ClientName;
this.isBuyer = bool.Parse(dsCommon[0].isBuyer.ToString());
this.isClient = bool.Parse(dsCommon[0].isClient.ToString());
this.isClientPowerUser = bool.Parse(dsCommon[0].powerUser.ToString());
this.isReportingUser = bool.Parse(dsCommon[0].reportingUser.ToString());
this.isSupplier = bool.Parse(dsCommon[0].isSupplier.ToString());
this.isPaperSupplier = bool.Parse(dsCommon[0].isPaperSupplier.ToString());
this.hasKitView = bool.Parse(dsCommon[0].haskitview.ToString());
}
public void loadUserInfo(int usersID)
{
this.ID = usersID;
loadUserInfo();
}
这是正确/标准的吗?或者我处理这个问题的方式不正确?目的是将 usersID 作为可选参数传递。
// Loads a users info
public void loadUserInfo()
{
CrystalTech.tblUsersDataTable dsCommon = new CrystalTech.tblUsersDataTable();
using (tblUsersTableAdapter userAdapter = new tblUsersTableAdapter())
{
userAdapter.FillBy(dsCommon, this.ID);
}
this.username = dsCommon[0].userName;
this.company.ID = dsCommon[0].clientID;
this.company.name = dsCommon[0].ClientName;
this.isBuyer = bool.Parse(dsCommon[0].isBuyer.ToString());
this.isClient = bool.Parse(dsCommon[0].isClient.ToString());
this.isClientPowerUser = bool.Parse(dsCommon[0].powerUser.ToString());
this.isReportingUser = bool.Parse(dsCommon[0].reportingUser.ToString());
this.isSupplier = bool.Parse(dsCommon[0].isSupplier.ToString());
this.isPaperSupplier = bool.Parse(dsCommon[0].isPaperSupplier.ToString());
this.hasKitView = bool.Parse(dsCommon[0].haskitview.ToString());
}
public void loadUserInfo(int usersID)
{
this.ID = usersID;
loadUserInfo();
}
Is this correct/standard? Or am I approaching this incorrectly? The aim is to have the usersID passed in an optional parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从 C# 4.0 开始,您还可以使用可选参数:
You can also use optional parameters as of C# 4.0:
首先,您所做的是
方法重载
,覆盖
是完全不同的事情。假设您正在追求方法重载,并且参数化方法的名称是一个简单的拼写错误,那么是的,这就是您的做法。Firstly what you have done is
method overloading
,overriding
is something completely different. Assuming it is method overloading you are after and the name of the parametered method is a simple typo, then yes that is how you do it.任何时候,只要您有名称相同但不同参数的方法,您就可以正确重载。
假设第二个方法是
loadUserInfo
那么是的 - 这是正确的。Any time you have methods with the same name but different parameters you are correctly overloading.
Assuming that second method is meant to be
loadUserInfo
then yes - this is correct.Tom,
这样做没有任何问题,但是您不必使用不同的方法名称来接受参数(loadUserInfo 与 loadUserInf)。如果两个不同的方法接受不同的参数,则它们可以具有相同的名称。所以你可以:
并且
Tom,
There's nothing wrong with doing it this way, but you don't have to have a different method name to accept the parameter (loadUserInfo vs. loadUserInf). You can have two different methods with the same name if they accept different parameters. So you could have:
AND
第二个应该是
loadUserInfo
但除此之外,是的。The second one should be
loadUserInfo
but otherwise, yes.如果您希望 ID 作为可选参数,则应查看 C# 4.0 中的 C# 可选参数
http://geekswithblogs.net/michelotti/archive/2009/02/05/c-4.0-optional-parameters.aspx
If you are wanting ID as an optional param, you should check out C#'s optional param that is in C# 4.0
http://geekswithblogs.net/michelotti/archive/2009/02/05/c-4.0-optional-parameters.aspx
不,不是。重写是指当您为继承的方法提供不同的实现时,您所做的就是重载。
重载时,应该对方法使用相同的名称。另外,建议使用 pascal 大小写命名方法:
No, it's not. Overriding is when you supply a different implementation for an inherited method, what you are doing is overloading.
When overloading, you should use the same name for the methods. Also, it's recommended to name methods using pascal case: