2

所以我一直在关注 C# 这本书。

http://www.robmiles.com/c-yellow-book/Rob%20Miles%20CSharp%20Yellow%20Book%202011.pdf 第 81-82 页我从那里获取此代码并从第 82 页添加另一种方法,结果是:

using System;      

enum AccountState
{
    New,
    Active,
    UnderAudit,
    Frozen,
    Closed
};

struct Account
{
    public AccountState State;
    public string Name;
    public string Address;
    public int AccountNumber;
    public int Balance;
    public int Overdraft;
};
class Bankprogram
{
    public static void Main()
    {   
        Account RobsAccount;    
        RobsAccount.State = AccountState.Active;    
        RobsAccount.Name = "Rob Miles";    
        RobsAccount.AccountNumber = 1234;    
        RobsAccount.Address = "his home";       
        RobsAccount.Balance = 0;    
        RobsAccount.Overdraft = -1;    
        Console.WriteLine("name is " + RobsAccount.Name);    
        Console.WriteLine("balance is : " + RobsAccount.Balance );      
    }
    public void PrintAccount(Account a)
    {
        Console.WriteLine ("Name" + a.Name);    
        Console.WriteLine ("Address :" + a.Address);    
        Console.WriteLine ("Balance:" + a.Balance);
    }

    PrintAccount(RobsAccount);
}

但我得到一个错误:方法必须有返回类型。指的是“PrintAccount(RobAccount);”

我知道以前有人问过这个问题,但没有一个看起来与我的问题相似。

4

7 回答 7

8

问题是编译器认为这PrintAccount(RobsAccount);是一个方法定义,这就是为什么需要返回类型。

您必须在另一个方法中调用该方法,不能在 void 上调用它。

于 2012-08-16T19:19:00.107 回答
4

有2个问题

问题 1:您的方法调用直接在类中。它必须在方法内部。我们在类中声明方法,而不是调用它们。

问题2:静态方法在静态方法中被调用,所以你的PrintAccount方法也应该是静态的。

解决方案:这应该是类结构。

//Previous code as it is
class Bankprogram
{
    public static void Main()
    {   
        //Previous code as it is
        PrintAccount(RobsAccount);
    }
    public static void PrintAccount(Account a)
    {
        //Method code as it is
    }  
}
于 2012-08-16T19:19:35.130 回答
1
PrintAccount(RobsAccount);

您希望何时准确调用该函数?您已将它放在类定义的中间(这就是您收到错误的原因)。您希望在程序启动时调用它吗?你的类的第一个实例是什么时候创建的?这没有意义;类是对象的模板。

如果你想调用一个方法,你必须从另一个方法中调用(暂时不考虑静态初始化)。因此,删除该行,然后在 main...

static void Main(...) 
{
    PrintAccount();
}

另请注意,您的设计很奇怪。您已经在Bankprogram类中定义了 main (为什么?)并且一切都是静态的。您是否只打算允许一个帐户,其中该帐户的每个属性都经过硬编码?似乎不是很有用。

于 2012-08-16T19:23:36.787 回答
0

问题是您在类中有代码,而不是在方法中。

将该行移到Main方法中:

using System; 


enum AccountState
{
  New,
  Active,
  UnderAudit,
  Frozen,
  Closed
};

struct Account
{
    public AccountState State;
    public string Name;
    public string Address;
    public int AccountNumber;
    public int Balance;
    public int Overdraft;
};

class Bankprogram
{

  public static void Main()
  {   
    Account RobsAccount;

    RobsAccount.State = AccountState.Active;

    RobsAccount.Name = "Rob Miles";

    RobsAccount.AccountNumber = 1234;

    RobsAccount.Address = "his home";

    RobsAccount.Balance = 0;

    RobsAccount.Overdraft = -1;

    Console.WriteLine("name is " + RobsAccount.Name);

    Console.WriteLine("balance is : " + RobsAccount.Balance );

    PrintAccount(RobsAccount);

  }

  public void PrintAccount(Account a)
  {
    Console.WriteLine ("Name" + a.Name);

    Console.WriteLine ("Address :" + a.Address);

    Console.WriteLine ("Balance:" + a.Balance);
  }

}

旁注:代码将 astruct用于数据对象,其中 aclass是合适的。

于 2012-08-16T19:21:04.883 回答
0

这里有两个问题;

  • 您正在尝试从类中调用您的 PrintAccount 方法,而不是从任何方法中。那显然行不通。
  • 您的 PrintAccount 方法不是静态方法,因此只能在您的类被实例化时访问。这意味着您将无法从 main 函数中调用它。

尝试更改以下两件事:

  • 使您的 PrintAccount 成为静态方法
  • 从您的主方法调用 PrintAccount 方法。

您的代码将如下所示:

public static void Main()
{   
    Account RobsAccount;
    RobsAccount.State = AccountState.Active;
    RobsAccount.Name = "Rob Miles";
    RobsAccount.AccountNumber = 1234;
    RobsAccount.Address = "his home";
    RobsAccount.Balance = 0;
    RobsAccount.Overdraft = -1;
    Console.WriteLine("name is " + RobsAccount.Name);
    Console.WriteLine("balance is : " + RobsAccount.Balance );
    PrintAccount(RobsAccount);
}

public static void PrintAccount(Account a)
{
    Console.WriteLine ("Name" + a.Name);
    Console.WriteLine ("Address :" + a.Address);
    Console.WriteLine ("Balance:" + a.Balance);
}

祝你好运!

于 2012-08-16T19:29:58.397 回答
0

首先,您试图PrintAccount()在任何其他方法之外调用方法!看来这是一个错字。

其次,PrintAccount()方法必须是静态的(它属于Bankprogram类),它是从静态Main()方法调用的。

class Bankprogram
{
    public static void Main()
    {   
        Account RobsAccount;

        RobsAccount.State = AccountState.Active;

        RobsAccount.Name = "Rob Miles";

        RobsAccount.AccountNumber = 1234;

        RobsAccount.Address = "his home";

        RobsAccount.Balance = 0;

        RobsAccount.Overdraft = -1;

        Console.WriteLine("name is " + RobsAccount.Name);

        Console.WriteLine("balance is : " + RobsAccount.Balance );

        PrintAccount(RobsAccount); // This line has been moved.
    }

    // This method has become STATIC!
    public static void PrintAccount(Account a)
    {
        Console.WriteLine ("Name" + a.Name);

        Console.WriteLine ("Address :" + a.Address);

        Console.WriteLine ("Balance:" + a.Balance);
    }
}
于 2012-08-16T19:18:38.270 回答
0

大概是笔误吧。不知道是你的还是书里的。

调用PrintAccount应该在 BankProgram 类的方法中进行。如所写,调用在任何方法之外,这是一个错误。
此外,如果从 Main 内部调用 PrintAccount 方法,也应该是静态的,因为要调用实例成员方法,您需要创建 BankProgram 类的实例

class Bankprogram 
{ 
    public static void Main() 
    {    
        Account RobsAccount; 
        RobsAccount.State = AccountState.Active; 
        RobsAccount.Name = "Rob Miles"; 
        RobsAccount.AccountNumber = 1234; 
        RobsAccount.Address = "his home"; 
        RobsAccount.Balance = 0; 
        RobsAccount.Overdraft = -1; 
        Console.WriteLine("name is " + RobsAccount.Name); 
        Console.WriteLine("balance is : " + RobsAccount.Balance ); 
        PrintAccount(RobsAccount);

        // OR - if you really want to keep NON STATIC the method PrintAccount
        // BankProgram bp = new BankProgram();
        // bp.PrintAccount(RobsAccount);
    } 

    public static void PrintAccount(Account a) 
    { 
        Console.WriteLine ("Name" + a.Name); 
        Console.WriteLine ("Address :" + a.Address); 
        Console.WriteLine ("Balance:" + a.Balance); 
    } 
} 
于 2012-08-16T19:20:07.363 回答