当前位置:网站首页>Go语学习笔记 - 语言接口 | 从零开始Go语言
Go语学习笔记 - 语言接口 | 从零开始Go语言
2022-04-23 06:33:00 【剑客阿良_ALiang】
学习笔记,写到哪是哪。
Go语言的接口和Java的接口有点类似,写法上不太一致。我个人感觉更像Java中父类中定义抽象方法类似,凡是集成父类都需要重新实现抽象方法。
样例代码如下
package main
import "fmt"
type Phone interface {
call(param int) string
info()
}
type IPhone struct {
name string
number string
}
func (iphone IPhone) call(param int) string {
return fmt.Sprintf("%d,%s", param, iphone.name)
}
func (iphone IPhone) info() {
fmt.Printf("我的手机信息为:name=%s,number=%s\n", iphone.name, iphone.number)
}
func main() {
var phone Phone = IPhone{"haha", "1234"}
phone.info()
phone.call(12311111)
}
执行结果
我的手机信息为:name=haha,number=1234
注意
1、接口的实现方式是在func前面括号指定结构体类型参数。
2、上面的样例代码中首先定义了Phone接口有两个方法call、info,IPhone结构体实现两个方法。
版权声明
本文为[剑客阿良_ALiang]所创,转载请带上原文链接,感谢
https://huyi-aliang.blog.csdn.net/article/details/124315075
边栏推荐
- MySQL -- the secret of lock -- how to lock data
- 关于unity获取真实地理地图转3D化的相关链接
- 一些关于网络安全的好教程或笔记的链接,记录一下
- GUI,CLI与Unix哲学
- Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
- 读书笔记
- Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
- TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
- VBA調用SAP RFC實現數據讀取&寫入
- Internal network security attack and defense: a practical guide to penetration testing (VII): cross domain attack analysis and defense
猜你喜欢
About USB flash drive data prompt raw, need to format, data recovery notes
SAP STO With Billing流程与配置
Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
nacos源码分析思路
MySQL--锁的奥秘--数据怎么锁
第四章 无形资产
BUUCTF [ACTF2020 新生赛]Include1
[NLP notes] preliminary study on CRF principle
Cloud computing skills competition -- Part 2 of openstack private cloud environment
How to present your digital portfolio: suggestions from creative recruiters
随机推荐
Guoji Beisheng openstack container cloud environment construction
面试学习路线
KCD_ EXCEL_ OLE_ TO_ INT_ Convert reports an error sy subrc = 2
内网渗透系列:内网隧道之dns2tcp
C smoothprogressbar custom progress bar control
CTF attack and defense world brush questions 51-
TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
Redis transaction implements optimistic locking principle
SAP tr manual import system operation manual
Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
VBA調用SAP RFC實現數據讀取&寫入
利用sqlmap注入获取网址管理员账号密码
Houdini地形与流体解算(模拟泥石流)
MYSQL——第一章节(数据类型2)
Analysis of Nacos source code
Complete color conversion formulas and conversion tables (31 kinds)
国基北盛-openstack-容器云-环境搭建
BUUCTF MISC刷題
Link to some good tutorials or notes about network security and record them
VBA调用SAP RFC实现数据读取&写入