go不存在继承,go里面叫组合,因为他和其他语言的继承很不一样,举个容易理解的例子。
package main
import "fmt"
type Base struct{}
func (b *Base) GetName() string {
return "base"
}
func (b *Base) PrintName() {
fmt.Println("++++++" + b.GetName() + "-----")
}
type A struct {
Base
}
func (a *A) GetName() string {
return "aaaa"
}
func main() {
a := A{}
a.PrintName()
}
输出为:++++++base-----
class Base(object):
def __init__(self):
self.name = "base"
def GetName(self):
return self.name
def PrintName(self):
print "+++++++" + self.GetName() + "----"
class A(Base):
def __init__(self):
super(A, self)
1
self.name = "aaa"
def GetName(self):
return self.name
if __name__ == '__main__':
a = A()
a.PrintName()
输出为:+++++++aaa----
结论: python里面的父类可以调用被子类重写的方法,但是go不可以,这就是区别。