type Person struct {
name string
age int
}
type Student struct {
Person
id int
score int
}
student := Student{Person{"jake",16},1001,99}
多态
package main
import "fmt"
type Human interface {
speak(language string)
}
type Chinese struct {
}
type American struct {
}
func (ch Chinese) speak(language string ) {
fmt.Printf("speck %s\n",language)
}
func (am American ) speak(language string ) {
fmt.Printf("speck %s\n",language)
}
func main() {
var ch Human
var am Human
ch = Chinese{}
am = American{}
ch.speak("Chinese")
am.speak("English")
}
继承与多态继承多态继承type Person struct { name string age int}type Student struct { Person id int score int}多态package mainimport "fmt"type Human interface { speak(language string)}type Chinese struct {}type American struct {}fun