【Go】面向对象(三):继承

2021-09-16 11:19:22 浏览数 (1)

代码语言:javascript复制
package main

import "fmt"

type person struct {
	name string
	age int
	sex bool
	hobby []string
}
func (p *person) Eat(){
	fmt.Printf("%s吃红烧肉n",p.name)
	fmt.Printf("%s吃大虾????n",p.name)
}

type coder struct{
	//↓继承person
	person
	langs []string
}
func main() {
	c:=new(coder)
	//coder的name属性继承自person
	c.name = "艾伦"
	c.langs = []string{"Go","Python","湖南话","C"}
	c.Eat()
	c.Code()
}

func (c *coder)Code()  {
	fmt.Printf("%s会%s,正在堆代码",c.name,c.langs)
}

0 人点赞