使用对象从非静态类访问静态方法。这在 C# 中是不可能的。它是由 JAVA 完成的。这个怎么运作?
java的例子
/**
* Access static member of the class through object.
*/
import java.io.*;
class StaticMemberClass {
// Declare a static method.
public static void staticDisplay() {
System.out.println("This is static method.");
}
// Declare a non static method.
public void nonStaticDisplay() {
System.out.println("This is non static method.");
}
}
class StaticAccessByObject {
public static void main(String[] args) throws IOException {
// call a static member only by class name.
StaticMemberClass.staticDisplay();
// Create object of StaticMemberClass class.
StaticMemberClass obj = new StaticMemberClass();
// call a static member only by object.
obj.staticDisplay();
// accessing non static method through object.
obj.nonStaticDisplay();
}
}
程序的输出:
This is static method.
This is static method.
This is non static method.
如何在 C# 中做到这一点? 提前致谢..