10.Go语言-面向对象简单了解

时间:2020-07-19 23:44:14   收藏:0   阅读:77

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
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!