golang继承,和多态

来源:互联网 发布:手机虚拟机软件 编辑:程序博客网 时间:2024/05/30 04:48
package maintype ST struct{}func (s *ST)Show(){    println("ST")}func (s *ST)Show2(){    println("ST:Show2()")}type ST2 struct{    ST    I int}func (s *ST2)Show(){    println("ST2")}func main() {    s := ST2{I:5}    s.Show()    s.Show2()    println(s.I)}

golang语言中没有继承,但是可以依靠组合来模拟继承和多态。

但是,这样模拟出来的继承是有局限的,也就是说:在需要多态的时候,需要小心。

原创粉丝点击