[英]Why must I use keyword static when they don't have to?
I've been trying simple experiments to learn C# methods. 我一直在尝试简单的实验来学习C#方法。 The below code simply calls playerSelection() which asks the user for a character and returns that character to Main(string[] args).
下面的代码只是调用playerSelection(),它向用户询问一个字符并将该字符返回给Main(string [] args)。 Main prints that to the console.
主要打印到控制台。 With the below code I get the following error "An object reference is required from a non-static field."
使用下面的代码我得到以下错误“非静态字段需要对象引用。”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace SimpleFunction
{
class Program
{
static void Main(string[] args)
{
char cplayerChoice = playerSelection();
Console.WriteLine(cplayerChoice);
}
char playerSelection()
{
Console.WriteLine("\nEnter a Character");
char cplayerChoice = Console.ReadKey().KeyChar;
return cplayerChoice;
}
}
}
Now if I add the word static like so: 现在如果我像这样添加单词static:
static char playerSelection()
it compiles and works. 它编译和工作。 I do understand static versus non...abstractly.
我确实理解静态与非......抽象。
I'm learning C# from a book and in that book they go through the below example to illustrate using methods: 我正在从一本书中学习C#,在那本书中,他们通过下面的例子来说明使用方法:
using System;
namespace GetinPaid
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
double dailyRate = readDouble("Enter your daily rate:");
int noOfDays = readInt("Enter the number of days: ");
writeFee(calculateFee(dailyRate, noOfDays));
}
private void writeFee(double p)
{
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private double calculateFee(double dailyRate, int noOfDays)
{
return dailyRate * noOfDays;
}
private int readInt(string p)
{
Console.Write(p);
string line = Console.ReadLine();
return int.Parse(line);
}
private double readDouble(string p)
{
Console.Write(p);
string line = Console.ReadLine();
return double.Parse(line);
}
}
}
Why in their example can they call methods without using the keyword static but I have to use it? 为什么在他们的例子中他们可以在不使用关键字static的情况下调用方法但我必须使用它?
Thanks! 谢谢!
In their example, they're creating an instance of Program
, and calling a method on that instance: 在他们的示例中,他们正在创建
Program
的实例 ,并在该实例上调用方法:
(new Program()).run();
This is more cleanly written as: 这更干净地写成:
Program program = new Program();
program.run();
From within those instance methods, you can call other instance methods because you're implicitly calling them on this
. 在这些实例方法中,您可以调用其他实例方法,因为您在
this
上隐式调用它们。
As an aside, if that really is sample code from the book, I suggest you get a different book: there are some very questionable aspects of style there. 顺便说一句,如果真的是书中的示例代码,我建议你得到一本不同的书:那里的风格有一些非常值得怀疑的方面。 In particular:
尤其是:
private
is explicit or implicit private
是明确的还是隐含的方面是不一致的 ReadInt32
instead of readInt
. ReadInt32
而不是readInt
。 Again, it's not as important for private methods, but it's a bad habit to get into double
for currency values is a really bad idea double
作为货币值是一个非常糟糕的主意 p
gives no information (in various places, used for different meanings) p
的参数名称不提供信息(在不同的地方,用于不同的含义) TryParse
rather than Parse
, and check the return value then potentially reprompt on bad input TryParse
而不是Parse
,并检查返回值然后可能在错误输入时重新启动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.