当前位置:网站首页>[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])
}
}

边栏推荐
- Detailed explanation of new features of ES advanced function syntax
- 图解LeetCode——640. 求解方程(难度:中等)
- The negative semantic transformation layer
- DOM-DOM树,一个DOM树有三种类型的节点
- 互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
- 一次简单的 JVM 调优,学会拿去写到简历里
- 程序化交易改变了什么?
- CC0 vs. commercial IP: which model is better for NFTs?
- 21 Day Learning Challenge Week 1 Summary
- [Pdf generated automatically bookmarks]
猜你喜欢

多商户商城系统功能拆解26讲-平台端分销设置

二叉树相关代码题【较全】C语言

CTO说MySQL单表行数不要超过2000w,为啥?

基于FPGA状态机的自动售货机功能实现

Multi-threaded ThreadPoolExecutor

Summary of debugging skills

Design and Realization of Employment Management System in Colleges and Universities

"How to kick a bad habit to read notes?

AI+医疗:使用神经网络进行医学影像识别分析

JS-DOM element object
随机推荐
互换性测量与技术——偏差与公差的计算,公差图的绘制,配合与公差等级的选择方法
分布式和集群的区别和联系
树莓派入门(5)系统备份
Talk about the understanding of RPC
学编程的第十三天
leetcode:358. K 距离间隔重排字符串
【LeetCode】Day112-repetitive DNA sequence
The most unlucky and the luckiest
C语言之自定义类型------结构体
一次简单的 JVM 调优,学会拿去写到简历里
深度学习-第二次
Window function application of sum and count
Salesforce解散中国团队,什么样的CRM产品更适合中国人
索引的创建、查看、删除
Design and Realization of Employment Management System in Colleges and Universities
AI+医疗:使用神经网络进行医学影像识别分析
Official release丨VS Code 1.70
Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
The problem that Merge will be lost again after code Revert has been solved
typedef定义结构体数组类型