Golang 繼承,多態

package main

import "fmt"

type P struct {
        PA string
        PB string
        PI interface{}
}

func (p *P) Read() error {
        fmt.Println("P.Read")
        fmt.Println("P.PI:",p.PI)
        return nil
}

type C struct {
        P
        PI string
}

func (c *C) Read() error {
        fmt.Println("C.Read.PA",c.PA)
        fmt.Println("PI:", c.PI)
return nil
}

func main() {
fmt.Println("Hello, World!")
        p := &P{PA:"PA",PB:"PB"}
        T := &C{P:*p,PI:"PI"}
        T.Read()
}

Hello, World!
C.Read.PA PA
PI: PI
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章