美文网首页
17 Golang结构体详解(三)

17 Golang结构体详解(三)

作者: learninginto | 来源:发表于2021-03-29 10:05 被阅读0次
    一、结构体嵌套
    • demo1
    type User struct {
        username string
        password string 
        sex string
        age int 
        address Address //User结构体嵌套Address结构体
    }
    
    type Address struct {
        name string
        phone string
        city string
    }
    
    func main() {
        var u User
        u.username = "lxc"
        u.password = "123456"
        u.address.name = "张三"
        u.address.phone = "010-1234567"
        u.address.city = "北京"
        fmt.Printf("%#v",u)
        //main.User{username:"lxc", password:"123456", sex:"", age:0, address:main.Address{name:"张三", phone:"010-1234567", city:"北京"}}
    }
    
    • 嵌套匿名结构体
    type User stuct {
        username string
        password string 
        Address
    }
    
    type Address struct {
        name string
        phone string
        city string
    }
    
    func main() {
        var u User
        u.city = "上海"
        fmt.Println(u.city)//上海
        fmt.Println(u.Address.city)//上海
    }
    
    • 关于嵌套结构体的字段名冲突(就近原则)
    type User struct {
        username string 
        password string
        addtime string
        Address
    }
    
    type Address struct {
        name string
        phone string
        city string
        addtime string
    }
    
    type Email struct {
        account string
        addtime string
    }
    
    func main () {
        var u User
        u.city = "上海"
        u.addtime = "2021-03-10"
        fmt.Println(u)
        //main.User{username:"", password:"", addtime:"2021-03-10", Address:main.Address{name:"",phone:"", city:"上海", addtime:""}}
        u.Address.addtime = "2021-06-10"
        fmt.Println(u)
    }
    
    • 当两个子结构同级、就近原则不法判断时,程序就会报错
    type User struct {
        username string
        password string
        Address
        Email
    }
    
    type Address struct {
        name string 
        phone string
        city string
        addtime string
    }
    
    type Email struct {
        account string
        addtime string
    }
    
    func main() {
        var u User
        //u.addtime = "2021-06-06"//报错 ambiguous selector u.addtime
        u.Address.addtime = "2021-06-06"
        u.Email.addtime = "2021-06-07"
        fmt.Println(u)
        //main.User{username:"", password:"", Address:main.Address{name:"", phone:"", city:"", addtime:"2021-06-06"}, Email:main.Email{account:"", addtime:"2021-06-07"}}
    }
    
    二、结构体的继承

    Go语言中,结构体的继承是通过嵌套实现的

    每次都从子结构体向父结构体中去找

    //父结构体
    type Animal struct {
        Name string
    }
    
    //子结构体
    type Dog struct {
        Age int
        Animal //结构体嵌套
    }
    
    func (a Animal) run() {
        fmt.Printf("%v在运动 ", a.Name)
    }
    
    func (d Dog) wang() {
        fmt.Printf("%v在汪汪 ", d.Name)
    }
    
    func main() {
        var d = Dog {
            Age : 20,
            Animal : Animal{
                Name : "旺财",
            },
        }
        d.run()
        d.wang()
        //旺财在运动 旺财在汪汪 
    }
    
    • 通过传递指针
    type Animal struct {
        Name string
    }
    
    type Dog struct {
        Age int
        *Animal //结构体嵌套
    }
    
    func (a Animal) run() {
        fmt.Printf("%v在运动 ", a.Name)
    }
    
    func (d Dog) wang() {
        fmt.Printf("%v在汪汪 ", d.Name)
    }
    
    func main() {
        var d = Dog {
            Age : 20,
            Animal : &Animal{
                Name : "旺财",
            },
        }
        d.run()
        d.wang()
        //旺财在运动 旺财在汪汪 
    }
    

    相关文章

      网友评论

          本文标题:17 Golang结构体详解(三)

          本文链接:https://www.haomeiwen.com/subject/hqgvhltx.html