gpt4 book ai didi

c# - static关键字是什么意思?

转载 作者:可可西里 更新时间:2023-11-01 08:37:30 27 4
gpt4 key购买 nike

我是 C# 初学者。我发现有两种方法可以编写代码并输出相同的结果。你能解释一下它们之间的区别吗?什么时候使用#1 和#2?

#1

class Program
{
static void Main()
{
Program min = new Program();
Console.WriteLine(min.isMin(1, 2));
Console.ReadLine();
}

int isMin(int value1, int value2)
{
int Min;
return Min = Math.Min(value1, value2);
}
}

#2

class Program2
{
static void Main()
{
Console.WriteLine(isMin(1, 2));
Console.ReadLine();
}

static int isMin(int value1, int value2)
{
int Min;
return Min = Math.Min(value1, value2);
}
}

最佳答案

#1和#2的区别在于#1中isMin是Program类的实例成员函数,因此必须创建Program类的实例

 Program min = new Program()

然后才调用实例成员函数isMin:

 min.isMin(..)


在#2中,isMin是Program类的静态成员函数,而Main也是同一个类的静态成员函数,所以可以在Main函数中直接调用isMin。

两者都有效。静态函数 Main 是程序的“入口点”,这意味着它首先被执行。剩下的只是面向对象的语义。

编辑

看来为了更好地说明这一点,应该举个例子。

除了显示将程序逻辑封装到对象与使用静态函数的替代方案之间的区别的预期目的之外,下面的两个程序毫无用处。

该程序定义了两个操作并将处理两个数字(示例中为 10 和 25)。当程序运行时,它会将其操作跟踪到一个日志文件(每个数字一个)。可以想象这两个操作可以被更严肃的算法代替,并且两个数字可以被一系列更有用的输入数据代替。

//The instance-based version:
class Program
{
private System.IO.StreamWriter _logStream;
private int _originalNumber;
private int _currentNumber;

public Program(int number, string logFilePath)
{
_originalNumber = number;
_currentNumber = number;
try
{
_logStream = new System.IO.StreamWriter(logFilePath, true);
_logStream.WriteLine("Starting Program for {0}", _originalNumber);
}
catch
{
_logStream = null;
}
}
public void Add(int operand)
{
if (_logStream != null)
_logStream.WriteLine("For {0}: Adding {1} to {2}", _originalNumber, operand, _currentNumber);
_currentNumber += operand;
}
public void Subtract(int operand)
{
if (_logStream != null)
_logStream.WriteLine("For {0}: Subtracting {1} from {2}", _originalNumber, operand, _currentNumber);
_currentNumber -= operand;
}
public void Finish()
{
Console.WriteLine("Program finished. {0} --> {1}", _originalNumber, _currentNumber);
if (_logStream != null)
{
_logStream.WriteLine("Program finished. {0} --> {1}", _originalNumber, _currentNumber);
_logStream.Close();
_logStream = null;
}
}

static void Main(string[] args)
{
Program p = new Program(10, "log-for-10.txt");
Program q = new Program(25, "log-for-25.txt");

p.Add(3); // p._currentNumber = p._currentNumber + 3;
p.Subtract(7); // p._currentNumber = p._currentNumber - 7;
q.Add(15); // q._currentNumber = q._currentNumber + 15;
q.Subtract(20); // q._currentNumber = q._currentNumber - 20;
q.Subtract(3); // q._currentNumber = q._currentNumber - 3;

p.Finish(); // display original number and final result for p
q.Finish(); // display original number and final result for q
}
}

以下是同一程序的基于静态函数的实现。请注意我们必须如何将我们的状态“携带”进出每个操作,以及 Main 函数如何需要“记住”哪些数据与哪个函数调用相关。

class Program
{
private static int Add(int number, int operand, int originalNumber, System.IO.StreamWriter logFile)
{
if (logFile != null)
logFile.WriteLine("For {0}: Adding {1} to {2}", originalNumber, operand, number);
return (number + operand);
}
private static int Subtract(int number, int operand, int originalNumber, System.IO.StreamWriter logFile)
{
if (logFile != null)
logFile.WriteLine("For {0}: Subtracting {1} from {2}", originalNumber, operand, number);
return (number - operand);
}
private static void Finish(int number, int originalNumber, System.IO.StreamWriter logFile)
{
Console.WriteLine("Program finished. {0} --> {1}", originalNumber, number);
if (logFile != null)
{
logFile.WriteLine("Program finished. {0} --> {1}", originalNumber, number);
logFile.Close();
logFile = null;
}
}

static void Main(string[] args)
{
int pNumber = 10;
int pCurrentNumber = 10;
System.IO.StreamWriter pLogFile;
int qNumber = 25;
int qCurrentNumber = 25;
System.IO.StreamWriter qLogFile;

pLogFile = new System.IO.StreamWriter("log-for-10.txt", true);
pLogFile.WriteLine("Starting Program for {0}", pNumber);
qLogFile = new System.IO.StreamWriter("log-for-25.txt", true);
qLogFile.WriteLine("Starting Program for {0}", qNumber);

pCurrentNumber = Program.Add(pCurrentNumber, 3, pNumber, pLogFile);
pCurrentNumber = Program.Subtract(pCurrentNumber, 7, pNumber, pLogFile);
qCurrentNumber = Program.Add(qCurrentNumber, 15, qNumber, qLogFile);
qCurrentNumber = Program.Subtract(qCurrentNumber, 20, qNumber, qLogFile);
qCurrentNumber = Program.Subtract(qCurrentNumber, 3, qNumber, qLogFile);

Program.Finish(pCurrentNumber, pNumber, pLogFile);
Program.Finish(qCurrentNumber, qNumber, qLogFile);
}
}

另一点需要注意的是,虽然第一个基于实例的示例有效,但在实践中更常见的是将您的逻辑封装在一个不同的类中,该类可以在您的程序的 Main 入口点中使用。这种方法更灵活,因为它可以很容易地获取您的程序逻辑并将其移动到不同的文件,甚至移动到甚至可以被多个应用程序使用的不同程序集。这是一种方法。

// Another instance-based approach
class ProgramLogic
{
private System.IO.StreamWriter _logStream;
private int _originalNumber;
private int _currentNumber;

public ProgramLogic(int number, string logFilePath)
{
_originalNumber = number;
_currentNumber = number;
try
{
_logStream = new System.IO.StreamWriter(logFilePath, true);
_logStream.WriteLine("Starting Program for {0}", _originalNumber);
}
catch
{
_logStream = null;
}
}
public void Add(int operand)
{
if (_logStream != null)
_logStream.WriteLine("For {0}: Adding {1} to {2}", _originalNumber, operand, _currentNumber);
_currentNumber += operand;
}
public void Subtract(int operand)
{
if (_logStream != null)
_logStream.WriteLine("For {0}: Subtracting {1} from {2}", _originalNumber, operand, _currentNumber);
_currentNumber -= operand;
}
public void Finish()
{
Console.WriteLine("Program finished. {0} --> {1}", _originalNumber, _currentNumber);
if (_logStream != null)
{
_logStream.WriteLine("Program finished. {0} --> {1}", _originalNumber, _currentNumber);
_logStream.Close();
_logStream = null;
}
}
}


class Program
{
static void Main(string[] args)
{
ProgramLogic p = new ProgramLogic(10, "log-for-10.txt");
ProgramLogic q = new ProgramLogic(25, "log-for-25.txt");

p.Add(3); // p._number = p._number + 3;
p.Subtract(7); // p._number = p._number - 7;
q.Add(15); // q._number = q._number + 15;
q.Subtract(20); // q._number = q._number - 20;
q.Subtract(3); // q._number = q._number - 3;

p.Finish();
q.Finish();
}
}

关于c# - static关键字是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/827424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com