当前位置:网站首页>[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
2022-08-11 03:20:00 【Yugong move code】
一、Interfaces and Inheritance and Conversion
package main
import "fmt"
type Humaner interface {
SayHello()
}
type Personer interface {
Humaner
Say()
}
type Student struct {
}
func (s *Student) SayHello() {
fmt.Println("大家好")
}
func (s *Student) Say() {
fmt.Println("你好")
}
func main() {
var stu Student
var per Personer
per = &stu
per.Say()
per.SayHello() // 可以调用所继承的接口中的方法.
var h Humaner
h = per
// per=h
h.SayHello()
}
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为.
Interface inheritance is a concept from design patterns.接口继承,subtyping.Describes when an object can be used in place of another object.接口是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.Interfaces can inherit multiple interfaces because interfaces only define behavior and do not contain the meaning of categories and do not refer to a specific kind of thing,
二、空接口
空接口(interface{})不包含任何的方法,正因为如此,所有的类型都实现了空接口,因此空接口可以存储任意类型的数值.
package main
import "fmt"
func main() {
var i interface{
}
i = 123
i = "abc"
fmt.Println(i)
var s []interface{
}
s = append(s, 123, "abc", 12.3)
for j := 0; j < len(s); j++ {
fmt.Println(s[j])
}
}
边栏推荐
- Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
- 电商项目——商城限时秒杀功能系统
- Qnet弱网测试工具操作指南
- 程序化交易与主观交易对盈利曲线的影响!
- 【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
- What does the sanction of the mixer Tornado mean for the DeFi market?
- "Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
- pathman_config、pathman_config_params 删除后,如何重建?
- 荣威imax8ev魔方电池安全感,背后隐藏着哪些黑化膨胀?
- Homework 8.10 TFTP protocol download function
猜你喜欢
最倒霉与最幸运
A practice arrangement about map GIS (below) GIS practice of Redis
Idea (preferred) cherry-pick operation
Official release丨VS Code 1.70
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
[DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
图解LeetCode——640. 求解方程(难度:中等)
How does MSP430 download programs to the board?(IAR MSPFET CCS)
Qnet弱网测试工具操作指南
基于FPGA状态机的自动售货机功能实现
随机推荐
LeetCode热题(12.买卖股票的最佳时机)
Google search skills - programmer is recommended
字体反扒
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
Summary of debugging skills
CTO说MySQL单表行数不要超过2000w,为啥?
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
《人生若如初见》命运多舛,人物饱满,朱亚文角色反差太惊喜
ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)
树莓派入门(5)系统备份
flink The object probably contains or references non serializable fields.
Salesforce解散中国团队,什么样的CRM产品更适合中国人
构建程序化交易系统需要注意什么问题?
聊聊对RPC的理解
Detailed explanation of new features of ES advanced array function syntax
typedef定义结构体数组类型
“顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?
增加对 Textbundle 的支持
Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票