对不起这个问题没有学问的性质.如果有一个简单的答案,只需要一个解释链接就会让我感到高兴.
编程6个月后,我发现静态类对于存储适用于许多不同类的例程有些用处.这是我如何使用静态类的简化示例,它是一个用于将文本解析为各种内容的类
public static class TextProcessor
{
public static string[] GetWords(string sentence)
{
return sentence.Split(' ');
}
public static int CountLetters(string sentence)
{
return sentence.Length;
}
public static int CountWords(string sentence)
{
return GetWords(sentence).Length;
}
}
Run Code Online (Sandbox Code Playgroud)
我用这个明显的方式使用它
class Program
{
static void Main(string[] args)
{
string mysentence = "hello there stackoverflow.";
Console.WriteLine("mysentence has {0} words in it, fascinating huh??", TextProcessor.CountWords(mysentence));
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么有必要将这些静态方法包装在静态类中?它似乎没有任何意义.有没有办法让我们可以将这些方法单独包装在一个类中?我知道封装是有益的,但我没有看到静态类包含静态方法的用法.有没有我风格或其他方面缺少的东西?我完全吠了一棵傻树吗?我想的太多了吗?
int i = 85;
Console.WriteLine("My intelligence quotient is {0}", i); // Kosher
MessageBox.Show("My intelligence quotient is {0}", i); // Not Kosher
Run Code Online (Sandbox Code Playgroud)
我发现这最令人痛苦的是让人衰弱.一个工作,而不是另一个?这种行为不协调的根源是什么?我想的越多,我的想象就越少,而且经常无法理解会变成自我厌恶.
据我所知,Equals()确定指定的对象是否等于当前对象.
所以如果我有这个Player类:
public class Player
{
int score;
public object Clone()
{
return this.MemberwiseClone();
}
public void SetScore(int i)
{
this.score = i;
}
public int GetScore()
{
return this.score;
}
}
Run Code Online (Sandbox Code Playgroud)
Ant然后我像这样实例化两个玩家:
Player p1 = new Player();
p1.SetScore(7);
Player p2 = (Player)p1.Clone();
Run Code Online (Sandbox Code Playgroud)
为什么Equals()在使用时会返回false:
Console.WriteLine(p1.Equals(p2)); // prints "False" to console
Run Code Online (Sandbox Code Playgroud)
他们怎么不平等?