'init'
This commit is contained in:
52
demo/test.go
Normal file
52
demo/test.go
Normal 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()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user