golang语言方法和函数
package mainimport "fmt"
func main() {
w1 := Worker{name: "王二狗", age: 30, sex: "男"}
w1.work()
w2 := Worker{name: "Ruby", age: 34, sex: "女"}
fmt.Printf("%T\n", w2)
w2.work()
w2.rest()
w1.rest()
w2.printInfo()
c1 := Cat{color: "白色的", age: 1}
c1.printInfo()
}
type Worker struct {
name string
ageint
sexstring
}
type Cat struct {
color string
age int
}
func (w Worker) work() {
fmt.Println(w.name, "在工作。。。")
}
func (p *Worker) rest() {
fmt.Println(p.name, "在休息。。。")
}
func (p Worker) printInfo() {
fmt.Printf("工人姓名1:%s,工人年龄:%d,工人性别:%s\n", p.name, p.age, p.sex)
}
func (p *Cat) printInfo() {
fmt.Printf("猫咪的颜色:%s,年龄:%d\n", p.color, p.age)
}
页:
[1]