通过嵌套接口嵌套结构体实现OOP
package main
import "fmt"
type aaa interface {
man()
}
type bbb interface {
women()
}
type tryinter interface {
aaa
bbb
}
type People struct {
name string
size float64
}
type Gender struct {
People
beard bool
//男: true | 女: false
}
type man struct {
Gender
money string
}
func (self man) man() {
fmt.Println(self.name, self.beard, self.money)
}
func (self Gender) women() {
fmt.Println(self.name, self.beard)
}
func main() {
Man := man{Gender{People{"张三 - 男人", 199.01}, true}, "10"}
Man.man()
Women := Gender{People{"李四 - 女人", 150.01}, false}
Women.women()
// 嵌套接口、嵌套结构体调用
var inter_Man tryinter
inter_Man = man{Gender{People{"接口 - 男人", 199.01}, true}, "一个亿"}
inter_Man.man()
}
接口断言
type jiegou struct{}
func inter_assers(x interface{}) {
switch v := x.(type) {
case int:
fmt.Printf("x是整数,值为: %v\n", v)
case string:
fmt.Printf("x是字符串,值为: %v\n", v)
case bool:
fmt.Printf("x是布尔,值为: %v\n", v)
case jiegou:
fmt.Printf("x是结构体,值为: %v\n", v)
case *string:
fmt.Printf("x是指针字符串,值为: %v\n", v)
default:
fmt.Println("找不到 type 类型")
}
}
func main() {
st := "字符串"
inter_assers(1)
inter_assers(st)
inter_assers(true)
inter_assers(jiegou{})
inter_assers(&st)
}