C# 中如何像 Lua 那样重写方法

所以在 Lua 中,您可以像这样做:

local button = CreateButton()
button:SetText("hello world")
button:SetPos(150, 20)
button.DoClick = function()
print("This button said hello")
end

现在我想知道在 C# 中是否可以做类似的事情 目前我有

Button button = new Button();
button.setPos(150, 20);
// 在这里需要类似 button.DoClick 的东西

在 C# 中是否有实现我想要的方法?

点赞
用户1187220
用户1187220

使用 C# 委托。

在您的情况下:

button.Click += ....

示例请参见:给动态添加的控件添加事件

对于其他函数:

button.Text = "hello world";

请参见 https://msdn.microsoft.com/en-us/library/ms158234(v=vs.110).aspx

button.Location = new Point(150, 20);

请参见 https://msdn.microsoft.com/en-us/library/system.windows.forms.control.location(v=vs.110).aspx

2017-01-20 15:25:09