Go不支持继承
类似继承,但是无法实现重载机制
package extension
import (
"fmt"
"testing"
)
type Pet struct {
}
func (p *Pet) Speack(){
fmt.Println("miao")
}
func (p *Pet) SpeakTo(){
p.Speack()
fmt.Println("...")
}
type Dog struct {
Pet
}
func (d *Dog) Speack(){
fmt.Println("wang")
}
func TestInterfacecha(t *testing.T){
var d = new(Dog)
d.SpeakTo() //输出还是miao
}
package extension
import (
"fmt"
"testing"
)
type Pet struct {
}
func (p *Pet) Speack(){
fmt.Println("miao")
}
func (p *Pet) SpeakTo(){
p.Speack()
fmt.Println("...")
}
type Dog struct {
p *Pet
}
func (d *Dog) Speack(){
fmt.Println("wang")
}
func TestInterfacecha(t *testing.T){
var d = new(Dog)
d.p.SpeakTo() //输出还是miao
}