创建了两个类Demo1和Demo2
Demo1里有一个字段sentence:
public class Demo1
{
public string sentence="OK";
}
Demo2的构造函数需要一个Demo1类型的对象作参数:
public class Demo2
{
string words;
//构造函数
public Demo2(Demo1 D)
{
words = D.sentence;
}
public void write()
{
Console.WriteLine(words);
}
}
在方法体外部实例化Demo2:
class Program
{
Demo1 D = new Demo1();
Demo2 D2 = new Demo2(D);
static void Main(string[] args)
{
D2.write();
Console.ReadKey();
}
}
会报错:
字段初始值设定项无法引用非静态字段、方法或属性
求大神们帮忙解惑,谢谢