GP43. 学生信息III
描述
一个结构体中可以嵌套包含另一个结构体或结构体指针。
输入描述
无输出描述
Go 解法, 执行用时: 2ms, 内存消耗: 832KB, 提交时间: 2022-07-17
package main import ( "fmt" ) type add struct{ province string city string } type student struct{ name string sex bool age int score int address add } func main() { xm:=student{ name:"小明", sex:true, age:23, score:88, address: add{ province:"湖南省", city:"长沙市", }, } fmt.Println(xm.name) fmt.Println(xm.sex) fmt.Println(xm.age) fmt.Println(xm.score) fmt.Println(xm.address.province) fmt.Println(xm.address.city) }
Go 解法, 执行用时: 2ms, 内存消耗: 936KB, 提交时间: 2022-06-17
package main import ( "fmt" ) type Address struct { Province string City string } type Student struct { name string sex bool age int score int Address } func main() { address := Address{Province: "湖南省", City: "长沙市"} stu := &Student{name: "小明", sex: true, age: 23, score: 88, Address: address} fmt.Println(stu.name) fmt.Println(stu.sex) fmt.Println(stu.age) fmt.Println(stu.score) fmt.Println(stu.Province) fmt.Println(stu.City) }
Go 解法, 执行用时: 2ms, 内存消耗: 1080KB, 提交时间: 2022-06-06
package main import ( "fmt" ) type addr struct { city string province string } // 结构体定义 type student struct { name string sex bool age int score int address addr } func main() { // 结构体的初始化 xiaoming := student { name: "小明", sex: true, age: 23, score: 88, address: addr{ city: "长沙市", province: "湖南省", }, } fmt.Println(xiaoming.name) fmt.Println(xiaoming.sex) fmt.Println(xiaoming.age) fmt.Println(xiaoming.score) fmt.Println(xiaoming.address.province) fmt.Println(xiaoming.address.city) }
Go 解法, 执行用时: 2ms, 内存消耗: 1196KB, 提交时间: 2022-06-08
package main import ( "fmt" ) type Student struct{ name string sex bool age int score int address addr } type addr struct{ province string city string } func main() { s := Student{ name: "小明", sex:true, age:23, score:88, address: addr{ city: "长沙市", province: "湖南省", }, } fmt.Println(s.name) fmt.Println(s.sex) fmt.Println(s.age) fmt.Println(s.score) fmt.Println(s.address.province) fmt.Println(s.address.city) }
Go 解法, 执行用时: 2ms, 内存消耗: 1196KB, 提交时间: 2022-06-03
package main import ( "fmt" ) type address struct { city string province string } type stu struct { name string sex bool age int score int add address } func main() { a := stu{ name: "小明", age: 23, sex: true, score: 88, add: address{ city: "长沙市", province: "湖南省", }, } fmt.Println(a.name) fmt.Println(a.sex) fmt.Println(a.age) fmt.Println(a.score) fmt.Println(a.add.province) fmt.Println(a.add.city) }