【Go进阶】详解嵌套struct中的方法

目录

(1)第一种方式

(2)第二种方式


(1)第一种方式

当内部struct嵌套进外部struct时,内部struct的方法也会被嵌套,也就是说外部struct拥有了内部struct的方法。

package main

import (
   "fmt"
)

type person struct{}

func (p *person) speak() {
   fmt.Println("speak in person")
}

// Admin exported
type Admin struct {
   person
   a int
}

func main() {
   a := new(Admin)

猜你喜欢

转载自blog.csdn.net/fanjufei123456/article/details/129948535