c# 这是我重写方法的方式吗?

发布于 2024-10-14 23:41:12 字数 1161 浏览 1 评论 0原文

    // 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 技术交流群。

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

发布评论

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

评论(7

且行且努力 2024-10-21 23:41:12

从 C# 4.0 开始,您还可以使用可选参数:

// Loads a users info
    public void loadUserInfo(int usersID = 0)
    {
        if (usersID > 0) this.ID = usersID;

        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());
    }

You can also use optional parameters as of C# 4.0:

// Loads a users info
    public void loadUserInfo(int usersID = 0)
    {
        if (usersID > 0) this.ID = usersID;

        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());
    }
安稳善良 2024-10-21 23:41:12

首先,您所做的是方法重载覆盖是完全不同的事情。假设您正在追求方法重载,并且参数化方法的名称是一个简单的拼写错误,那么是的,这就是您的做法。

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.

稚气少女 2024-10-21 23:41:12

任何时候,只要您有名称相同不同参数的方法,您就可以正确重载。

假设第二个方法是 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.

天赋异禀 2024-10-21 23:41:12

Tom,

这样做没有任何问题,但是您不必使用不同的方法名称来接受参数(loadUserInfo 与 loadUserInf)。如果两个不同的方法接受不同的参数,则它们可以具有相同的名称。所以你可以:

public void loadUserInfo()

并且

public void loadUserInfo(int usersID)

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:

public void loadUserInfo()

AND

public void loadUserInfo(int usersID)
拥抱没勇气 2024-10-21 23:41:12

第二个应该是 loadUserInfo 但除此之外,是的。

The second one should be loadUserInfo but otherwise, yes.

流云如水 2024-10-21 23:41:12

如果您希望 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

此岸叶落 2024-10-21 23:41:12

不,不是。重写是指当您为继承的方法提供不同的实现时,您所做的就是重载。

重载时,应该对方法使用相同的名称。另外,建议使用 pascal 大小写命名方法:

// Loads a users info
public void LoadUserInfo() {
  ...
}

public void LoadUserInfo(int usersID) {
  this.ID = usersID;
  loadUserInfo();
}

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:

// Loads a users info
public void LoadUserInfo() {
  ...
}

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