'init'
This commit is contained in:
37
demo/demo3.go
Normal file
37
demo/demo3.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Animal struct {
|
||||
Age int
|
||||
Weight float32
|
||||
}
|
||||
|
||||
func (an *Animal) shou() {
|
||||
fmt.Println("打击")
|
||||
}
|
||||
func (an *Animal) showinfo() {
|
||||
fmt.Println("动物的体重", an.Age, an.Weight)
|
||||
|
||||
}
|
||||
|
||||
type Cat struct {
|
||||
Animal
|
||||
Age int
|
||||
}
|
||||
|
||||
func (c *Cat) showInfo() {
|
||||
fmt.Println("动物体重", c.Age, c.Weight)
|
||||
|
||||
}
|
||||
|
||||
// 对cat绑定特有的方法
|
||||
func (c *Cat) rs() {
|
||||
fmt.Println("我是小猫")
|
||||
}
|
||||
func main() {
|
||||
cat := &Cat{}
|
||||
cat.Weight = 10
|
||||
cat.Animal.Age = 30
|
||||
fmt.Println(cat)
|
||||
}
|
||||
Reference in New Issue
Block a user