在 Ruby 中,我们可以通过以下方式创建类方法:
def self.class_method
"This is a class method"
end
def instance_method
"This is an instance method"
end
如何在 C# 中创建类方法?
在 Ruby 中,我们可以通过以下方式创建类方法:
def self.class_method
"This is a class method"
end
def instance_method
"This is an instance method"
end
如何在 C# 中创建类方法?
....static
和非static
..
public class YourObject {
public static void MyClassMethod() { // "class method" is a static method
}
public void MyInstanceMethod() {
}
}
// callable as..
YourObject.MyClassMethod();
// or..
YourObject obj = new YourObject();
obj.MyInstanceMethod(); // OK
任何教程都涵盖了这个..
public class YourClass
{
public string Foo()
{
return "Hello World"
}
}
YourClass yourClass = new YourClass();
Console.Write(yourClass.Foo());