当前位置:网站首页>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
边栏推荐
- CTF-MISC总结
- A programmer who works four hours a day
- 第四章 无形资产
- Sto with billing cross company inventory dump return
- Using lambda expression to solve the problem of C file name sorting (whether it is 100 or 11)
- Houdini地形与流体解算(模拟泥石流)
- Talk about the essence of interface idempotent and consumption idempotent
- Dvwa 靶场练习记录
- 【编程实践/嵌入式比赛】嵌入式比赛学习记录(一):TCP服务器和web界面的建立
- Search and replacement of C text file (WinForm)
猜你喜欢

DVWA靶场练习

Feign源码分析

国基北盛-openstack-容器云-环境搭建

Redis -- why is the string length of string emstr the upper limit of 44 bytes?

Houdini > variable building roads, learning process notes

Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack

Intranet penetration series: pingtunnel of Intranet tunnel

Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)

STO With Billing 跨公司库存转储退货

Sto with billing cross company inventory dump return
随机推荐
《内网安全攻防:渗透测试实战指南》读书笔记(四):权限提升分析及防御
About USB flash drive data prompt raw, need to format, data recovery notes
Buctf MISC brossage
Attack and defense world misc questions 1-50
VBA調用SAP RFC實現數據讀取&寫入
《内网安全攻防:渗透测试实战指南》读书笔记(八):权限维持分析及防御
云计算技能大赛 -- openstack私有云环境 第一部分
内网渗透系列:内网隧道之icmpsh
随笔(不定时更新)
Cloud computing skills competition -- Part 2 of openstack private cloud environment
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
SAP TR手动导入系统操作手册
Houdini流体>>粒子流体导出到unity笔记
DVWA靶场练习
Buuctf misc brush questions
Link to some good tutorials or notes about network security and record them
Post of experience in preparation for guarantee and research -- the 18th (2021) Central South planning department promoted the exemption to Zhejiang University Institute of Technology
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
Ctf-misc summary
A programmer who works four hours a day