【发布时间】:2019-04-16 14:49:45
【问题描述】:
我正在尝试在 A 类中实现一个接口方法,通过该实现我想从输入中给出变量 g 值,然后能够在其他派生类中读取该 g 值。问题是,没有派生类能够看到该值。似乎是什么问题?
interface ISomething
{
void something(string some);
}
public class A : ISomething
{
public string g;
public void something(string some)
{
g = some;
}
}
public class B : A
{
public void methodB()
{
Console.WriteLine($"Printing g value from method B: {g}");
}
}
public class C : A
{
public void methodC()
{
Console.WriteLine($"Printing g value from method C: {g}");
}
}
public class D : B
{
public void methodD()
{
Console.WriteLine($"Printing g value from method D: {g}");
}
}
static void Main(string[] args)
{
Console.WriteLine("Input something: ");
string x = Console.ReadLine();
A a = new A();
a.something(x);
B b = new B();
C c = new C();
D d = new D();
b.methodB();
c.methodC();
d.methodD();
Console.ReadKey();
}
【问题讨论】:
标签: class variables interface implementation derived