方法必须具有返回类型(C#)
所以我一直跟着C#写这本书 在第81-82页,我从那里得到了这段代码,并从第82页添加了另一个方法,结果是:方法必须具有返回类型(C#),c#,methods,return,C#,Methods,Return,所以我一直跟着C#写这本书 在第81-82页,我从那里得到了这段代码,并从第82页添加了另一个方法,结果是: using System; enum AccountState { New, Active, UnderAudit, Frozen, Closed }; struct Account { public AccountState State; public string Name; public string
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);
}
但我得到一个错误:方法必须有返回类型。指“打印帐户(RobAccount);”
我知道以前有人问过这个问题,但没有一个问题与我的问题相似。首先,您试图在任何其他方法之外调用
printcount()
方法!这似乎是个打字错误
其次,printcount()
方法必须是静态的(它属于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);
}
}
问题是编译器认为
printcount(RobsAccount)
是一个方法定义,这就是为什么需要返回类型
您必须在另一个方法中调用该方法,不能在void中调用它。有两个问题
问题1:方法调用直接在类内部。它必须在一个方法中。我们在类中声明方法,而不是调用它们
问题2:静态方法在静态方法中调用,因此您的printcount
方法也应该是静态的
解决方案:这应该是类结构
//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
}
}
可能是打字错误。不知道是你的还是书上的 对
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);
}
}
问题是类中有代码,而这些代码不在方法中 将该行移到
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);
}
}
旁注:代码对数据对象使用了结构
,其中类
是合适的
PrintAccount(RobsAccount);
您希望什么时候准确地调用该函数?您已经在类定义中使用了它(这就是为什么您会得到错误)。您希望在程序启动时调用它吗?类的第一个实例何时创建?这没有意义;类是对象的模板
如果你想调用一个方法,你必须从另一个方法调用(静态初始化暂时搁置)。所以,去掉那条线,然后在主
static void Main(...)
{
PrintAccount();
}
还请注意,您的设计非常奇怪。您已经在
Bankprogram
类中定义了main(为什么?)。您是否只打算允许一个帐户,其中该帐户的每个属性都是硬编码的?似乎不是很有用。这里有两件事不对
- 您试图从类而不是任何方法调用PrintAccount方法。这显然行不通
- PrintAccount方法不是静态方法,因此只能在实例化类时访问。这意味着您将无法从主函数调用它
- 将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);
}
祝你好运 您正试图直接在类内部调用方法。你不能那样做。试着把你的
PrintAccount(RobsAccount)放进去
位于Main
方法的底部。您的句子PrintAccount(RobsAccount)代码>不在method@Serge:是的,可能是这样,但那将是下一步,与这个问题无关。这不是一个很好的例子…谢谢你的帮助,但现在我遇到了另一个问题。它告诉我“非静态字段、方法或属性‘Bankprogram.PrintAccount(Account)’需要对象引用”@user1604146:MakePrintAccount
method为静态。我答案中的第二个问题。它来自一本介绍C#@user1604146的书:啊,这更有意义。。。我不知道这本书试图在这里教什么,但你可能想从现在开始带着一点怀疑的态度来看待它。