1.面向对象
1.1匿名字段
package main
import "fmt"
type Person struct {
name string
sex string
age int
}
type Student struct {
Person
id int
addr string
}
func main() {
s1:=Student{Person{"Simi","man",20}, 1, "china"}
fmt.Print(s1)
s2:=Student{Person:Person{"alex","woman",30}}
fmt.Print(s2)
s3 := Student{Person:Person{name:"wming"}}
fmt.Println(s3)
}
1.2接口
package main
import "fmt"
type Person struct {
name string
age int
sex string
}
type mystr string
type Student struct {
*Person
int
mystr
}
func main() {
s1:=Student{&Person{"xujk",20,"man"},22,"hello world"}
fmt.Println(s1)
fmt.Println(s1.name)
fmt.Println(s1.Person.name)
}
{0xc000066180 22 hello world}
xujk
xujk
|
请发表评论