我可以使用分部类修改函数,例如在一个分部类中声明它并在另一个分部类中定义它
public partial class PartialClass
{
public void showa()
{
}
}
public partial class PartialClass
{
string b = "b";
public void showa()
{
Console.Write(b);
}
}
Run Code Online (Sandbox Code Playgroud)
或者在另一个部分类中扩展函数的功能
public partial class PartialClass
{
int a = 10;
public void showa()
{
Console.WriteLine(a);
}
}
public partial class PartialClass
{
string b = "b";
public void showa()
{
Console.WriteLine(a+b);
}
}
Run Code Online (Sandbox Code Playgroud)
是的,但您也需要使用部分方法:
public partial class PartialClass
{
public void showa()
{
showaImpl();
}
partial void showaImpl();
}
public partial class PartialClass
{
string b = "b";
partial void showaImpl()
{
Console.Write(b);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果代码仅使用前半部分编译 - 那么这showa()是一个空方法.如果showaImpl声明了实现 - 那么调用就存在了.如果没有实现,编译器将完全忽略对部分方法的调用- 因此他们不能拥有访问修饰符,或者具有返回值或参数(因为这会使调用者明确赋值为噩梦).out