【问题标题】:C# - Cannot create an instance of the abstract class or interface [duplicate]C# - 无法创建抽象类或接口的实例[重复]
【发布时间】:2014-08-17 16:45:29
【问题描述】:

我在 C# 教程中收到错误 Cannot create an instance of the abstract class or interface

这条线失败了:result = new Account(nameText, addressText, balance);

这是我的课:

public abstract class Account : IAccount
{
    //---------------------------------------------------------------
    // Constructor
    //---------------------------------------------------------------
    public Account(string inName, string inAddress, decimal inBalance)
    {
        name = inName;
        address = inAddress;
        balance = inBalance;
    }
    public Account(string inName, string inAddress) :
        this(inName, inAddress, 0)      // 'this ties this alternate constructor back to the original constructor (directly above)
    {
    }
    public Account(string inName) :     // 'this ties this alternate constructor back to the original constructor (directly above)
        this(inName, "Not Supplied", 0)
    {
    }


    //---------------------------------------------------------------
    // Properties
    //---------------------------------------------------------------
    //  *       *       *       *       *       *       *       *
    //global account constraints
    private static decimal minIncome = 10000;
    private static int minAge = 18;

    //  *       *       *       *       *       *       *       *
    //personal details
    private string name;
    private string address;

    //  *       *       *       *       *       *       *       *
    //account details
    public int AccountNumber;
    public static decimal InterestRateCharged;
    public AccountState State;
    private decimal balance = 0;
    public int Overdraft;


    //---------------------------------------------------------------
    // Methods
    //---------------------------------------------------------------
    // loads the account
    public static Account Load(string filename)
    {
        Account result = null;
        System.IO.TextReader textIn = null;

        try
        {
            textIn = new System.IO.StreamReader(filename);
            string nameText = textIn.ReadLine();
            string addressText = textIn.ReadLine();
            string balanceText = textIn.ReadLine();
            decimal balance = decimal.Parse(balanceText);
            result = new Account(nameText, addressText, balance);
        }
        catch
        {
            return null;
        }
        finally
        {
            if (textIn != null) textIn.Close();
        }
        return result;
    }

};

这是我的界面:

public interface IAccount
{
    // account info
    int GetAccountNumber();
    string GetName();
    decimal GetBalance();

    // account actions
    void PayInFunds(decimal amount);
    bool WithdrawFunds(decimal amount);
    string RudeLetterString();
}

【问题讨论】:

  • 如果要实例化这个类为什么是抽象的?
  • 错误的哪一部分你不明白?
  • 您确定您甚至阅读了错误消息吗?即使您不这样做,您也可以将其直接复制并粘贴到谷歌中,这将为您提供大量结果,准确解释它是什么、为什么会发生以及如何解决它。

标签: c# class oop interface


【解决方案1】:

这就是抽象类的定义。您不能创建它的实例,只能创建从它派生的类型。

想想水果之类的东西。你不能把水果放在桌子上。它必须是某种类型的水果,例如橙子或苹果。在这种情况下,fruit 将是一个抽象类,并且可以实现所有水果共有的功能和属性。 OrangeApple 将派生自 Fruit 类,并实现特定于该 type 水果的功能和属性。

如果创建类的实例没有意义,但您希望实现可跨派生类型共享的基本功能,请使用 abstract 关键字。如果你只是想定义一个没有任何共享代码的合约,你会使用一个接口。

【讨论】:

  • 如果你解释为什么有一个抽象的水果的概念而不是你需要一个具体的水果的限制,我认为第二段会更有帮助.特别是抽象类与接口有何不同。
  • @Mike Christensen,感谢您的解释。 +1。
  • @ConradFrix - 感谢您的建议,我已经更新了我的答案。我想保持简单,以避免信息过载。
  • 说到抽象类,我很喜欢这个例子。男孩,我希望当我第一次遇到抽象类时就向我展示了这一点。
  • +1。喜欢Mike Christensen的详细描述,比一般的技术文档详细多了,水果的概念大家都能看懂! :) 干得好,迈克,但你可以包含一个简短的示例,说明我们如何通过创建类的实例、实现类中标记的抽象函数以及可能解释 VIRTUAL 函数在上课,像这样设置......(嘿......只是破坏你的排骨!......你的描述仍然很棒!)干得好!
【解决方案2】:
public abstract class Account : IAccount

去掉abstract这个词

如果您只想从静态方法创建 Account 实例,请改为使用构造函数 privateprotected

【讨论】:

  • 感谢您的解释。 +1。
猜你喜欢
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多