关于Go语言不支持继承的问题

419 views
Skip to first unread message

Yili Zhao

unread,
Nov 2, 2011, 8:34:04 PM11/2/11
to golang-china
传统的面向对象语言(如C++、Java和C#)支持类型的继承,以达到代码重用和多态的目的。我看了Go语言的文档,好像并不支持类型的继承。我觉得采用类型继承也有其优点,比如XNA框架,其基本的一个类型继承设计为:
namespace Framework
{
public class Game
{
protected virtual void update()
{
// Game类的更新操作
}
protected virtual void Draw()
{
// Game类的绘制操作
}
public void Run()
{
while (true)
{
this.Update();
this.Draw();
}
}
}
}

如果用户要实现自己的逻辑,可以通过继承来实现:
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

mikespook

unread,
Nov 3, 2011, 5:03:31 AM11/3/11
to golang...@googlegroups.com
这种其实用不到继承吧?只要实现个接口就好了。对于公用方法,封装到 Run 方法里,或者将函数作为参数传递进 update和draw。

type Game interface {
update()
draw()
}

func run(game Game, perup func(), postup func()) {
 for{
  perup()
  game.update()
  postup()
  draw()
 }
}

Marvin Guo

unread,
Nov 4, 2011, 1:11:41 AM11/4/11
to golang...@googlegroups.com
传统的C系语言,从simula过来的语言,有一名经典的话

继承是为了多态

对于Go,继承不是为了多态,设计框架的人,只是设计相应接口,接口相应函数,实现者不用从接口继承,只要实现是接口里的函数,就可以匹配。


2011/11/3 Yili Zhao <pan...@gmail.com>

Yili Zhao

unread,
Nov 5, 2011, 5:11:26 AM11/5/11
to golang...@googlegroups.com
用继承的目的有两个:
1. 代码重用
把一些通用的代码放到父类Game的方法里面,例如把事件循环放到“run”方法里面。这样,子类MyGame继承Game,无需再写相同的代码;
2. 多态
父类定义一些标注为"virtual"的方法,例如"Update"和"Draw“。子类可以”override“这些虚拟方法,添加自己的实现,然后有需要的话,通过”base.XXX()"调用父类同名的方法。
这种策略,Go语言如何实现?

zhai

unread,
Nov 5, 2011, 10:47:16 AM11/5/11
to golang...@googlegroups.com
使用匿名字段,

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:

  • If S contains an anonymous field T, the method set of S includes the method set of T.
  • If 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).
  • If 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).


2011/11/5 Yili Zhao <pan...@gmail.com>

zhai

unread,
Nov 5, 2011, 10:59:09 AM11/5/11
to golang...@googlegroups.com


2011/11/5 Yili Zhao <pan...@gmail.com>

用继承的目的有两个:
1. 代码重用
把一些通用的代码放到父类Game的方法里面,例如把事件循环放到“run”方法里面。这样,子类MyGame继承Game,无需再写相同的代码;
 
package main
import "fmt"
type A struct{}

func (self *A)do() {
  fmt.Println("A.do")  
}

type A1 struct {
A
}
func main(){
var a A1
a.do()  
}
 
2. 多态
父类定义一些标注为"virtual"的方法,例如"Update"和"Draw“。子类可以”override“这些虚拟方法,添加自己的实现,然后有需要的话,通过”base.XXX()"调用父类同名的方法。
这种策略,Go语言如何实现?

package main
import "fmt"

type A struct{
}

func (self *A)do() {
  fmt.Println("A.do")  
}

type A1 struct {
A
}
func (self *A1) do() {
  fmt.Println("A1.do")
}
func main(){
var a A1
a.do()     // 这是override
a.A.do()  //这是base.Do
}

qq wu

unread,
Nov 5, 2011, 2:01:02 PM11/5/11
to golang...@googlegroups.com
不要把其它语言的使用方式套到Go上,struct的mixin和面向对象的继承有本质区别,要多态就用interface.

zhai

unread,
Nov 5, 2011, 10:10:15 PM11/5/11
to golang...@googlegroups.com
用interface达不到Yili Zhao所说的那种效果

mikespook

unread,
Nov 7, 2011, 12:03:14 AM11/7/11
to golang...@googlegroups.com

Howard Fan

unread,
Nov 7, 2011, 1:59:42 AM11/7/11
to golang...@googlegroups.com

这个好玩,谢谢

Reply all
Reply to author
Forward
0 new messages