重载抽象方法

发布于 2024-09-29 21:28:28 字数 756 浏览 0 评论 0原文

考虑这个例子:

public interface IAccount
{
    string GetAccountName(string id);
}

public class BasicAccount : IAccount
{
    public string GetAccountName(string id)
    {
        throw new NotImplementedException();
    }

}

public class PremiumAccount : IAccount
{
    public string GetAccountName(string id)
    {
        throw new NotImplementedException();
    }

    public string GetAccountName(string id, string name)
    {
        throw new NotImplementedException();
    }
}

protected void Page_Load(object sender, EventArgs e)
{

    IAccount a = new PremiumAccount();

    a.GetAccountName("X1234", "John"); //Error
}

如何从客户端调用重写的方法,而不必在抽象/接口上定义新的方法签名(因为这只是高级帐户的特殊情况)?我在这个设计中使用抽象工厂模式...谢谢...

Consider this example:

public interface IAccount
{
    string GetAccountName(string id);
}

public class BasicAccount : IAccount
{
    public string GetAccountName(string id)
    {
        throw new NotImplementedException();
    }

}

public class PremiumAccount : IAccount
{
    public string GetAccountName(string id)
    {
        throw new NotImplementedException();
    }

    public string GetAccountName(string id, string name)
    {
        throw new NotImplementedException();
    }
}

protected void Page_Load(object sender, EventArgs e)
{

    IAccount a = new PremiumAccount();

    a.GetAccountName("X1234", "John"); //Error
}

How can I call the overridden method from the client without having to define a new method signature on the abstract/interface (since it is only an special case for the premium account)? I'm using abstract factory pattern in this design... thanks...

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

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

发布评论

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

评论(4

把梦留给海 2024-10-06 21:28:28

您必须将接口强制转换为特定的类。请记住,这会将接口的整个概念抛到九霄云外,并且您可以在所有情况下使用特定的类。考虑调整您的架构。

You will have to cast the interface to the specific class. Keep in mind that this would throw the whole concept of interfaces right out of the window and you could be using specific classes in all cases. Think about adjusting your architecture instead.

丿*梦醉红颜 2024-10-06 21:28:28

您将引用强制转换为特定类型:

((PremiumAccount)a).GetAccountName("X1234", "John");

You cast the reference to the specific type:

((PremiumAccount)a).GetAccountName("X1234", "John");
雨落星ぅ辰 2024-10-06 21:28:28

您可以使用这两种方法定义 IPremiumAccount 接口,并在 PremiumAccount 类中实现它。检查对象是否实现接口可能比检查特定基类更好。

public interface IPremiumAccount : IAccount
{
    public string GetAccountName(string id, string name);
}

public class PremiumAccount : IPremiumAccount
{

// ...

IAccount a = factory.GetAccount();
IPremiumAccount pa = a as IPremiumAccount;
if (pa != null)
    pa.GetAccountName("X1234", "John");

You can define IPremiumAccount interface with both methods and implement it the PremiumAccount class. Checking if an object implements the interface is probably better than checking for specific base class.

public interface IPremiumAccount : IAccount
{
    public string GetAccountName(string id, string name);
}

public class PremiumAccount : IPremiumAccount
{

// ...

IAccount a = factory.GetAccount();
IPremiumAccount pa = a as IPremiumAccount;
if (pa != null)
    pa.GetAccountName("X1234", "John");
场罚期间 2024-10-06 21:28:28

好吧,考虑到它仅为 PremiumAccount 类型定义,您知道可以调用它的唯一方法是 a 实际上是一个 PremiumAccount,对吧?因此,首先转换为 PremiumAccount

IAccount a = new PremiumAccount();

PremiumAccount pa = a as PremiumAccount;
if (pa != null)
{
    pa.GetAccountName("X1234", "John");
}
else
{
    // You decide what to do here.
}

Well, considering it's only defined for the PremiumAccount type, the only way you know you can call it is if a is actually a PremiumAccount, right? So cast to a PremiumAccount first:

IAccount a = new PremiumAccount();

PremiumAccount pa = a as PremiumAccount;
if (pa != null)
{
    pa.GetAccountName("X1234", "John");
}
else
{
    // You decide what to do here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文