如果用户要实现自己的逻辑,可以通过继承来实现:
namespace MyNamespace
{
public class MyGame : Game
{
protected override void update()
{
// MyGame类的更新操作
base.Update();
}
protected override void Draw()
{
// Game类的绘制操作
base.Draw();
}
public static void Main(string[] args)
{
MyGame mg = new MyGame();
mg.Run();
}
}
}
如果使用Go语言,如何实现这样的框架设计?
--
Yili Zhao
Fields and methods (§Method declarations) of an anonymous field are promoted to be ordinary fields and methods of the struct (§Selectors). The following rules apply for a struct type named S and a type named T:
S contains an anonymous field T, the method set of S includes the method set of T.S contains an anonymous field *T, the method set of S includes the method set of *T (which itself includes the method set of T).S contains an anonymous field T or *T, the method set of *S includes the method set of *T (which itself includes the method set of T).用继承的目的有两个:1. 代码重用把一些通用的代码放到父类Game的方法里面,例如把事件循环放到“run”方法里面。这样,子类MyGame继承Game,无需再写相同的代码;
2. 多态父类定义一些标注为"virtual"的方法,例如"Update"和"Draw“。子类可以”override“这些虚拟方法,添加自己的实现,然后有需要的话,通过”base.XXX()"调用父类同名的方法。这种策略,Go语言如何实现?
这个好玩,谢谢