1. 程式人生 > >golang繼承,和多型

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語言中沒有繼承,但是可以依靠組合來模擬繼承和多型。

但是,這樣模擬出來的繼承是有侷限的,也就是說:在需要多型的時候,需要小心。