标签:string fun print running 形参 package func 关键字 name
package main
import "fmt"
type Person struct {
//和变量定义不同,不要写var关键字
Id int
Name string
Sex byte
}
type Student struct {
Person
Hobby string
}
func (s Person) PrintValue() {
fmt.Println("该方法receiver is person")
}
func (s Student) PrintValue() {
fmt.Println("该方法receiver is student")
}
func (s *Person) Printpointer() {
s.Name = "steven"
fmt.Println("该方法receiver is *person")
}
func main() {
s1 := Student{Person{1, "steven", ‘m‘}, "running"}
//方法值,相当于方法指针
FuPrint := s1.PrintValue
FuPrint()
//方法表达式,显示的传参(这里没有继承概念)
FuPrint1 := (Student).PrintValue //变量类型名(Student)作为函数的传递形参,实现显示的传递
FuPrint1(s1)
}
标签:string fun print running 形参 package func 关键字 name
原文地址:https://www.cnblogs.com/zhaopp/p/11565457.html