代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"math/rand"
)
type Student struct {
Name string
Age int
}
func (p *Student) init(Name string, Age int) {
p.Name = Name
p.Age = Age
}
func (p *Student) String() string {
str := fmt.Sprintf("name=%s,age=%d\n", p.Name, p.Age)
return str
}
func testString() { //String接口的实现
var stu *Student = new(Student)
stu.Name = "hu"
stu.Age = 23
fmt.Printf("%s", stu)
}
type Test interface {
Print()
Sleep()
}
func (p *Student) Print() {
fmt.Printf("name=%s,age=%d\n", p.Name, p.Age)
}
func (p *Student) Sleep() {
fmt.Printf("I am sleeping for %f minutes\n", rand.Float32()*60)
}
func testInterface() {
// var t Test=&Student{
// Name:"zhang",
// Age:25,
// }
var t Test
var stu *Student = new(Student)
stu.Name = "zhang"
stu.Age = 25
t = stu
t.Print()
t.Sleep()
}
type People struct {
Name string
Age int
}
func (people *People) Print() {
fmt.Printf("name=%s,age=%d\n", people.Name, people.Age)
}
func (people *People) Sleep() {
fmt.Printf("I am sleeping for %f minutes\n", rand.Float32()*60)
}
func testInterface2() { //借口里面有方法,可以实现(含有这个借口所有方法的 )结构体
var people *People = &People{
Name: "zhou",
Age: 34,
}
var t Test
t = people
t.Print()
t.Sleep()
}
func PrintInterface() { //借口是个指针
var t Test
fmt.Println(t)
}
type Test2 interface { //借口可以嵌套
Test
Eat()
}
func (people *People) Eat() {
fmt.Println("I am having breakfast.")
}
func testInterface3() {
var people *People = &People{
Name: "sun",
Age: 35,
}
var t Test = people
var t2 Test2 = people
t.Sleep()
t.Print()
t2.Sleep()
t2.Print()
t2.Eat()
}
func main() {
// var p *Student = new(Student)
// p.init("hu", 23)
// fmt.Println(*p)
// testString()
// testInterface()
// testInterface2()
// PrintInterface()
testInterface3()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。