- package main
- import (
- "fmt"
- )
- func main() {
- stu := Student{
- base: Base{Id: 1, Name: "student"},
- School: "桃村一中",
- }
- fmt.Printf("%p, %v\\n", &stu, &stu)
- stu.toString()
- stu.toString2()
- fmt.Println("==============")
- tea := Teacher{
- base: Base{Id: 2, Name: "teacher"},
- Project: "数学",
- }
- fmt.Printf("%p, %v\\n", &tea, &tea)
- tea.toString()
- tea.toString2()
- //fmt.Println("base tea",tea)fmt.Println(tea.type())
- }
- type Base struct {
- Id int
- Name string
- }
- type Student struct {
- base Base
- School string
- }
- type Teacher struct {
- base Base
- Project string
- }
- func (self *Student) toString() {
- fmt.Println("*Student ")
- fmt.Printf("%p", self)
- fmt.Println(" Student ", self.base.toString(), " School", self.School)
- }
- func (self Student) toString2() {
- fmt.Println(" Student ")
- fmt.Printf("%p", &self)
- fmt.Println(self)
- // fmt.Println("Student ", self.base.toString(), " School", self.School)
- }
- func (self *Base) toString() string {
- return fmt.Sprintf("Id:", self.Id, "Name:", self.Name)
- }
- func (self Teacher) toString2() {
- fmt.Println("Teacher ")
- fmt.Printf("%p", &self)
- fmt.Println(self)
- }
- func (self *Teacher) toString() {
- fmt.Println("*Teacher ")
- fmt.Printf("%p", self)
- fmt.Println("Teacher ", self.base.toString, " Project", self.Project)
- }
- //该片段来自于http://www.codesnippet.cn/detail/0312201514187.html
来源: http://www.codesnippet.cn/detail/0312201514187.html