Golang之interface(多态,类型断言)
多态用法
- package main
- //一种事物的多种形态,都可以按照统一的接口进行操作
- //多态
- import (
- "fmt"
- "math/rand"
- "sort"
- )
- type Student struct {
- Name string
- Id string
- Age int
- sortType int
- }
- type Book struct {
- Name string
- Author string
- }
- //切片默认传地址
- type StudentArray []Student
- func (p StudentArray) Len() int {
- return len(p)
- }
- func (p StudentArray) Less(i, j int) bool {
- return p[i].Name < p[j].Name
- }
- func (p StudentArray) Swap(i, j int) {
- p[i], p[j] = p[j], p[i]
- }
- func main() {
- var stus StudentArray
- for i := ; i < ; i++ {
- stu := Student{
- Name: fmt.Sprintf("stu%d", rand.Intn()),
- Id: fmt.Sprintf("110%d", rand.Int()),
- Age: rand.Intn(),
- }
- stus = append(stus, stu)
- }
- for _, v := range stus {
- fmt.Println(v)
- }
- fmt.Println("\n\n")
- sort.Sort(stus)
- for _, v := range stus {
- fmt.Println(v)
- }
- }
接口嵌套
- package main
- import "fmt"
- //接口嵌套 一个接口可以嵌套在另外的接口
- type Reader interface {
- Read()
- }
- type Writer interface {
- Write()
- }
- type ReadWriter interface {
- Reader
- Writer
- }
- type File struct {
- }
- func (f *File) Read() {
- fmt.Println("read data")
- }
- func (f *File) Write() {
- fmt.Print("write data")
- }
- func Test(rw ReadWriter) {
- rw.Read()
- rw.Write()
- }
- func main() {
- var f File
- Test(&f)
- }
类型断言
- package main
- import "fmt"
- type Student struct {
- Name string
- Sex string
- }
- //类型断言
- //一个判断传入参数类型的函数
- func just(items ...interface{}) {
- for index, v := range items {
- switch v.(type) {
- case bool:
- fmt.Printf("%d params is bool,value is %v\n", index, v)
- case int, int64, int32:
- fmt.Printf("%d params is int,value is %v\n", index, v)
- case float32, float64:
- fmt.Printf("%d params is float,value is %v\n", index, v)
- case string:
- fmt.Printf("%d params is string,value is %v\n", index, v)
- case Student:
- fmt.Printf("%d params student,value is %v\n", index, v)
- case *Student:
- fmt.Printf("%d params *student,value is %v\n", index, v)
- }
- }
- }
- func main() {
- var b Student = Student{
- Name: "stu01",
- Sex: "female",
- }
- just(, 8.2, "this is a test", b, &b)
- }
Golang之interface(多态,类型断言)的更多相关文章
- Go interface{}、类型断言
在 golang 中 interface{} 可用于向函数传递任意类型的变量, 但在函数内部使用的话, 该变量的类型就是 interface{}, 也称为空接口类型 比如我们定义一个函数, 输出字符串 ...
- Golang-interface(四 反射)
github:https://github.com/ZhangzheBJUT/blog/blob/master/reflect.md 一 反射的规则 反射是程序执行时检查其所拥有的结构.尤其是类型的一 ...
- golang学习笔记:Interface类型断言详情
原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...
- golang 类型断言的学习
在php中有一个 serialize() 函数 可以把数组序列化成字符串进行存储和传输 如果想反序列化这种字符串,在php中只需要一个简单的unserialize() 函数就可以完成了.但是在gola ...
- golang类型断言
一.介绍 类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言 例子: package main import "fmt" func main(){ ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- Golang | 既是接口又是类型,interface是什么神仙用法?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第12篇文章,我们来继续聊聊interface的使用. 在上一篇文章当中我们介绍了面向对象的一些基本概念,以及gol ...
- [Go] golang类型断言
类型断言有点像向下转型,接口类型转到具体的实现实例类型上类型断言是一个使用在接口值上的操作.语法上它看起来像x.(T)被称为断言类型,这里x表示一个接口的类型和T表示一个类型 package main ...
- Golang的类型断言
类型断言即判断一个变量是不是某个类型的实例,这个经常用在判断接口的类型,基本的格式: y, ok := x.(type) 上面的语句用于判断变量x是不是type类型,有两种结果: x是type类型的变 ...
随机推荐
- BZOJ4372: 烁烁的游戏【动态点分治】
Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠. 题意: 给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠. 烁烁他每次会跳到一个节点u,把周围与他距离不超过d的节点各吸引出w ...
- Oracle单表去重复(二)
Oracle单表去重 去重有两层含义,一:是记录完全一样.二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而 ...
- sublime text 3 配置优化
1.在用Sublime里编写代码时,需要把TAB键转换成四个空格. 菜单栏里点击:首选项-> 设置-用户(Preferences-> Setting-User)在弹出来的文本里,添加如下两 ...
- rsync 通过密码文件实现远程同步
https://my.oschina.net/yyping/blog/91964 1.源文件服务器:192.168.10.203 2.备份服务器:192.168.10.88 配置备份服务器(192.1 ...
- [C++ Primer] 第7章: 类
定义抽象数据类型 定义在类内部的函数是隐式的inline函数. const成员函数 又叫做常量成员函数, 成员函数参数列表之后紧跟const关键字, const修饰的是类this指针. 默认情况下th ...
- Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找
ylbtech-Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找 1.返回顶部 1. Java 实例 - 字符串搜索 Java 实例 以下实例使用了 Strin ...
- expect学习笔记及用法
expect学习笔记及实例详解 expect的基本用法 expect用法
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
- JDK、Spring、Quartz等几种不同定时器的用法,以及cronExpression表达式定义
referenc:https://blog.csdn.net/clementad/article/details/42042111 下面介绍几种常用的定时器及其实现方法: 第一种:Timer和Time ...
- predict.glm -> which class does it predict?
Jul 10, 2009; 10:46pm predict.glm -> which class does it predict? 2 posts Hi, I have a question a ...