golang继承,和多态

时间:2025-02-24 12:55:16
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.Show2()
    println()
}

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

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