This commit is contained in:
2024-08-01 23:40:06 +08:00
commit 1adca2ffe6
17 changed files with 469 additions and 0 deletions

52
demo/test.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import "fmt"
// 接口定义设计规范
type SayHello interface {
SayHello()
}
type American struct {
Name string
}
func (person American) SayHello() {
fmt.Println("你好")
}
type chinese struct {
Name string
}
func (person chinese) SayHello() {
fmt.Println("chinese")
}
func (person chinese) tiaowu() {
fmt.Println("跳舞")
}
// 定义函数:接收接口的实例
func greet(s SayHello) {
s.SayHello()
//断言控制语句的要求 判断这是chinese
ch, flag := s.(chinese)
if flag == true {
ch.tiaowu()
} else {
fmt.Println("no")
}
}
func main() {
var a [3]SayHello
a[0] = chinese{"1"}
a[1] = American{"hel"}
a[0].SayHello()
c := chinese{}
c.tiaowu()
c.SayHello()
}