方法和变量
1.方法的重载
跟Java一样的机制,默认情况下可以override,如果要把某个方法设为不可override,则要使用关键字final,要把某个类设为不可继承,也使用该关键字(C#里是sealed)
2.支持嵌入函数
void MyMethod()
{
void MyFunction() //Embedded function only has scope within
MyMethod
{
//some statements
}
MyFunction();
}
void MyMethod()
{
void MyFunction() //Embedded function only has scope within
MyMethod
{
//some statements
}
MyFunction();
}
{
void MyFunction() //Embedded function only has scope within
MyMethod
{
//some statements
}
MyFunction();
}
这个在C++,Java,C#中都不支持.不过貌似意义不是太大。
3.静态成员
class MyClass
{
//Variables could go here
static void MyMethod()
{
// some statements
}
void MyMethod()
{
// some statements
}
}
class MyClass
{
//Variables could go here
static void MyMethod()
{
// some statements
}
void MyMethod()
{
// some statements
}
}
{
//Variables could go here
static void MyMethod()
{
// some statements
}
void MyMethod()
{
// some statements
}
}
这个在C#里是不允许的,static不属于方法的签名,这两个方法属于同名方法。
调用
void SomeMethod()
{
MyClass mc = new MyClass();
mc.MyMethod(); /* The method in the current instance of the class (the object referred to as mc) is invoked.*/
mc::MyMethod(); // The static method in the class is invoked
}
{
MyClass mc = new MyClass();
mc.MyMethod(); /* The method in the current instance of the class (the object referred to as mc) is invoked.*/
mc::MyMethod(); // The static method in the class is invoked
}