我在一个类中有静态变量和方法。它们是否会在派生类中被继承?
例如:
class A
{
public static int x;
public static void m1()
{
some code
}
}
class B:A
{
B b=new B();
b.m1(); // will it be correct or not, or will I have to write
// new public voim1(); or public void m1();
b.x=20; // will it be correct or not?
}
最佳答案
静态成员将在派生类中可用,但您不能使用实例引用访问它们。您可以直接访问它们:
m1();
x = 20;
或者使用类名:
A.m1();
A.x = 20;
关于c# - 静态方法和变量在派生类中可用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4067104/