-- 定义名为 AppBase 的基础类
1. function AppBase:ctor(name)
2. self.name =name
3. if self.name then
4. print("打印名字:"..self.name)
5. end
6. self:onCreate()
7. end
8. --为AppBase定义一个onCreate()方法
9. function AppBase:onCreate()
10. print("执行进来了")
11. end
12. --为AppBase定义一个initView()方法
13. function AppBase:initView()
14. print("创建视图")
15. end
16. --定义一个MyApp类继承与AppBase
17. local MyApp = class("MyApp", AppBase)
18. function MyApp:ctor()
19. --如果继承覆盖了ctor()构造函数,那必须手动调用父类得构造函数
20. --类名.super可以访问指定得父类
21. MyApp.super.ctor(self,"MyApp")
22. self:initView()
23. end
24. function MyApp:initView()
25. print("覆盖父类的同名方法")
26. end
27.
Lua类创建与继承
最新推荐文章于 2024-07-19 21:35:46 发布