33_ using the same name field

In the structure, if the structure contains inherited from the parent structure, if the same name field contains, in addition to the fields which indicate the specific structure of the outer part of, the default action: the principle of proximity

package main
 
import "fmt"
 
// definition of a structure, store student information
// To call the name of a different package structure, the structure of the first letter to be bigger, but also inside the variable name in uppercase
type Student struct {
	// define variables and different, do not write the var keyword
	Id   int
	Name string
	Sex change
	Age  int
	Addr string
}
type mystr string
type Person struct {
	// inherit student body structure
	Student // so-called anonymous field is write-only structure (variable) name, rather than write their type
	Name    string
	string // anonymous field, built-in type
	mystr // custom fields
}
 
func main() {
	the P3 Personal
	p3.Name = "zhao"
	fmt.Println("p3=", p3) //p3= {{0  0 0 } zhao  }
 
	// operations of the same name inherited field
	p3.Student.Name = "wang"
	fmt.Println("p3=", p3) //p3= {{0 wang 0 0 } zhao  }
 
	//initialization
	p4 := Person{Student{1, "li", 'w', 24, "sd"}, "hao", "jia", "steven"}
	fmt.Println("p4=", p4) //p4= {{1 li 119 24 sd} hao jia steven}
	fmt.Println(p4.mystr)  //steven
 
}

Guess you like

Origin www.cnblogs.com/zhaopp/p/11565224.html