golang继承,和多态
发布时间:2020-12-16 19:28:09 所属栏目:大数据 来源:网络整理
导读: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语言
package main type 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语言中没有继承,但是可以依靠组合来模拟继承和多态。 但是,这样模拟出来的继承是有局限的,也就是说:在需要多态的时候,需要小心。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |