忽略所有内容直到第二次编辑
我正在尝试做这样的事情:
public interface IModifier{
public String nameTag;
public void foo();
}
Run Code Online (Sandbox Code Playgroud)
我尝试这样做的原因是:我有一个类 SpecificModifier 实现了 IModifier 并且有许多非常相似的类也实现了 IModifier。我希望每个实现 IModifier 的类都有一个公共字符串 nameTag。
编辑:我已经确认我不能这样做,但是有人可以解释为什么接口不能需要字段吗?
编辑二:
我对抽象类与接口的目的的理解。接口纯粹用于声明实现它的任何内容的必要部分,以便所有对象都有可以引用的公共部分。而抽象类用于为多个类提供通用功能。
这有点过于简单化了,但无论如何,除了语言设计者的疏忽之外,我仍然没有理由认为接口不能具有抽象字段。
任何人都可以提供一个原因吗?
private static readonly List<long> KnownPrimes = new List<long>() { 2, 3, 5, 7};
static void Main(string[] args)
{
int numDivisors;
string input = "";
bool first = true;
while (!int.TryParse(input, out numDivisors))
{
if(!first) Console.WriteLine("You must enter a number with no other characters.");
Console.WriteLine("Find the least common multiple for numbers 1 through:");
input = Console.ReadLine();
first = false;
}
int index = -1;
//make sure that there are enough primes in the list
while (index == -1)
{
index = …Run Code Online (Sandbox Code Playgroud)