当前位置:网站首页>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
边栏推荐
- Reading notes
- DVWA靶场练习
- 1+x云计算中级--脚本搭建读写分离
- MySQL--锁的奥秘--数据怎么锁
- 数据库之MySQL——基础篇
- How to present your digital portfolio: suggestions from creative recruiters
- Houdini > variable building roads, learning process notes
- The displayed amount of ABAP ALV is inconsistent with the exported amount
- Unity get real geographic map application terrain notes
- SAP STO With Billing流程与配置
猜你喜欢

Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >

Mysql database backup and recovery under Linux (full + incremental)

How to present your digital portfolio: suggestions from creative recruiters

Houdini地形与流体解算(模拟泥石流)

内网渗透系列:内网隧道之icmp_tran

upload-labs 靶场练习

Ctf-misc summary

nacos源码分析思路

Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight

内网渗透系列:内网隧道之icmptunnel(DhavalKapil师傅的)
随机推荐
内网渗透系列:内网隧道之icmp_tran
About unity to obtain links related to the transformation of real geographic maps into 3D
第七章 资产减值
随笔(不定时更新)
When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.
Feign源码分析
Houdini > rigid body, rigid body breaking RBD
KCD_ EXCEL_ OLE_ TO_ INT_ Convert reports an error sy subrc = 2
SAP tr manual import system operation manual
Attack and defense world misc questions 1-50
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
The displayed amount of ABAP ALV is inconsistent with the exported amount
[unity VFX] Introduction notes of VFX special effects - spark production
upload-labs 靶场练习
SAP TR手动导入系统操作手册
Essays (updated from time to time)
Cloud computing skills competition -- the first part of openstack private cloud environment
strcat()、strcpy()、strcmp()、strlen()
GUI,CLI与Unix哲学
Suggestions on university learning route planning