当前位置:网站首页>【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
2022-08-11 03:14:00 【愚公搬代码】
文章目录
一、接口和继承和转换
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来声明。接口可以多继承接口因为接口只定义行为并不包含类别的含义不指代具体的某类事物,
二、空接口
空接口(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])
}
}
边栏推荐
猜你喜欢
[BX] and loop
互换性与测量技术——表面粗糙度选取和标注方法
CTO说MySQL单表行数不要超过2000w,为啥?
A practice arrangement about map GIS (below) GIS practice of Redis
The practice of alibaba data synchronization component canal
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
Idea (优选)cherry-pick操作
字体反扒
DNS分离解析和智能解析
VIT 源码详解
随机推荐
Traversal of DOM tree-----modify styles, select elements, create and delete nodes
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
Idea (优选)cherry-pick操作
df和df -lh的意思
【LeetCode】Day112-重复的DNA序列
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
对加密世界的经济误解:现金是储蓄?稀缺性创造价值?
关于地图GIS开发事项的一次实践整理(上)
Idea (preferred) cherry-pick operation
21 Day Learning Challenge Week 1 Summary
BUU brushing record
最倒霉与最幸运
正式发布丨VS Code 1.70
①In-depth analysis of CAS SSO single sign-on framework source code
高度塌陷问题的解决办法
互换性与测量技术——表面粗糙度选取和标注方法
Deep Learning - Second Time
CSAPP Data Lab
E-commerce project - mall time-limited seckill function system