10.Go语言-面向对象简单了解
2021-04-13 12:29
标签:href pre int ring main pac mys struct code 10.Go语言-面向对象简单了解 标签:href pre int ring main pac mys struct code 原文地址:https://www.cnblogs.com/xujunkai/p/13341629.html1.面向对象
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
上一篇:11.Go语言-接口