• Golang 基础


    通过嵌套接口嵌套结构体实现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
    ", v)
    	case string:
    		fmt.Printf("x是字符串,值为: %v
    ", v)
    	case bool:
    		fmt.Printf("x是布尔,值为: %v
    ", v)
    	case jiegou:
    		fmt.Printf("x是结构体,值为: %v
    ", v)
    	case *string:
    		fmt.Printf("x是指针字符串,值为: %v
    ", v)
    	default:
    		fmt.Println("找不到 type 类型")
    	}
    }
    
    func main() {
    	st := "字符串"
    	inter_assers(1)
    	inter_assers(st)
    	inter_assers(true)
    	inter_assers(jiegou{})
    	inter_assers(&st)
    }
    
    作者:GI-JOE
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    图书管理系统时序图和活动图
    图书管理用例图
    课堂练习(团队)
    课堂练习(RASCI模型)
    课堂练习(分析设想app)
    课堂练习(NABCD model)
    课堂练习(问答题)
    测试用例
    课堂练习(测试计划)
    错误报告
  • 原文地址:https://www.cnblogs.com/BenLam/p/15428053.html
Copyright © 2020-2023  润新知