Golang OOP、继承、组合、接口
Class Human {
name:string
age:int
function eat(){}
function speak(){}
} Class Man extends Human {
function fish(){}
function drink(){}
}
package main import "fmt" func main(){
var h Human s := Student{Grade: 1, Major: "English", Human: Human{Name: "Jason", Age: 12, Being: Being{IsLive: true}}}
fmt.Println("student:", s)
fmt.Println("student:", s.Name, ", isLive:", s.IsLive, ", age:", s.Age, ", grade:", s.Grade, ", major:", s.Major) //h = s // cannot use s (type Student) as type Human in assignment
fmt.Println(h) //Heal(s) // cannot use s (type Student) as type Being in argument to Heal
Heal(s.Human.Being) // true s.Drink()
s.Eat()
} type Car struct {
Color string
SeatCount int
} type Being struct {
IsLive bool
} type Human struct {
Being
Name string
Age int
} func (h Human) Eat(){
fmt.Println("human eating...")
h.Drink()
} func (h Human) Drink(){
fmt.Println("human drinking...")
} func (h Human) Move(){
fmt.Println("human moving...")
} type Student struct {
Human
Grade int
Major string
} func (s Student) Drink(){
fmt.Println("student drinking...")
} type Teacher struct {
Human
School string
Major string
Grade int
Salary int
} func (s Teacher) Drink(){
fmt.Println("teacher drinking...")
} type IEat interface {
Eat()
} type IMove interface {
Move()
} type IDrink interface {
Drink()
} func Heal(b Being){
fmt.Println(b.IsLive)
}
输出结果:
student: {{{true} Jason 12} 1 English}
student: Jason , isLive: true , age: 12 , grade: 1 , major: English
{{false} 0}
true
student drinking...
human eating...
human drinking...
这里有一点需要注意,Student 实现了 Drink 方法,覆盖了 Human 的 Drink,但是没有实现 Eat 方法。因此,Student 在调用 Eat 方法时,调用的是 Human 的 Eat();而 Human 的 Eat() 调用了 Human 的 Drink(),于是我们看到结果中输出的是 human drinking... 。这既不同于 Java 类语言的行为,也不同于 prototype 链式继承的行为,Golang 叫做 Embedding,这像是一种寄生关系:Human 寄生在 Student 中,但仍保持一定程度的独立。
interface IEnglishSpeaker {
ListenEnglish()
ReadEnglish()
SpeakEnglish()
WriteEnglish()
}
// 接上面的例子 v1, b := interface{}(s).(Car)
fmt.Println(v1, b) v2, b := interface{}(s).(Being)
fmt.Println(v2, b) v3, b := interface{}(s).(Human)
fmt.Println(v3, b) v4, b := interface{}(s).(Student)
fmt.Println(v4, b) v5, b := interface{}(s).(IDrink)
fmt.Println(v5, b) v6, b := interface{}(s).(IEat)
fmt.Println(v6, b) v7, b := interface{}(s).(IMove)
fmt.Println(v7, b) v8, b := interface{}(s).(int)
fmt.Println(v8, b)
{ 0} false
{false} false
{{false} 0} false
{{{true} Jason 12} 1 English} true
{{{true} Jason 12} 1 English} true
{{{true} Jason 12} 1 English} true
<nil> false
0 false
上面的代码中,使用空接口 interface{} 对 s 进行了类型转换,因为 s 是 struct,不是 interface,而类型断言表达式要求点号左边必须为接口。
常用的方式应该是类似泛型的使用方式:
s1 := Student{Grade: 1, Major: "English", Human: Human{Name: "Jason", Age: 12, Being: Being{IsLive: true}}}
s2 := Student{Grade: 1, Major: "English", Human: Human{Name: "Tom", Age: 13, Being: Being{IsLive: true}}}
s3 := Student{Grade: 1, Major: "English", Human: Human{Name: "Mike", Age: 14, Being: Being{IsLive: true}}}
t1 := Teacher{Grade: 1, Major: "English", Salary: 2000, Human: Human{Name: "Michael", Age: 34, Being: Being{IsLive: true}}}
t2 := Teacher{Grade: 1, Major: "English", Salary: 3000, Human: Human{Name: "Tony", Age: 31, Being: Being{IsLive: true}}}
t3 := Teacher{Grade: 1, Major: "English", Salary: 4000, Human: Human{Name: "Ivy", Age: 40, Being: Being{IsLive: true}}}
drinkers := []IDrink{s1, s2, s3, t1, t2, t3} for _, v := range drinkers {
switch t := v.(type) {
case Student:
fmt.Println(t.Name, "is a Student, he/she needs more homework.")
case Teacher:
fmt.Println(t.Name, "is a Teacher, he/she needs more jobs.")
default:
fmt.Println("Invalid Human being:", t)
}
}
Jason is a Student, he/she needs more homework.
Tom is a Student, he/she needs more homework.
Mike is a Student, he/she needs more homework.
Michael is a Teacher, he/she needs more jobs.
Tony is a Teacher, he/she needs more jobs.
Ivy is a Teacher, he/she needs more jobs.
这段代码中使用了 Type Switch,这种 switch 判断的目标是类型。
Golang OOP、继承、组合、接口的更多相关文章
- python面向对象编程 继承 组合 接口和抽象类
1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均用点来访问自己的 ...
- Py修行路 python基础 (十五)面向对象编程 继承 组合 接口和抽象类
一.前提回忆: 1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均 ...
- Golang的面向对象编程【结构体、方法、继承、接口】
Golang也支持面向对象编程.但与以前学过传统的面向对象编程语言有区别.1)Golang没有类class,Go语言的结构体struct和类class有相似的特性.2)Golang中不存在继承,方法重 ...
- Python 继承和组合 接口
#解决代码重用的问题,减少代码冗余 #继承是一种什么'是'什么的关系 class People: def __init__(self, name, age): # print('People.__in ...
- 对java中继承、接口、组合的思考
1.在c++中有继承和多重继承,而java中只有单继承.继承的好处在于可以复用一些东西,但缺陷在于后续不好扩展.此外,可读性方面继承也不好. 2.java中多了一个接口的概念,而接口的功能和其名字表达 ...
- GOLANG的继承+接口语法练习
继承与接口同时存在 在Golang语言中,可以这么说:接口是继承的功能补充! 武当派有一个徒弟结构体,它继承WuDangMaster结构体的字段及方法 武林之中还有一个泰山北斗,名约少林派,少林入门神 ...
- 第一单元总结:基于基础语言、继承和接口的简单OOP
前情提要 到目前为止,OO课程已经完成了前三次的作业,分别为: 第一次作业:简单多项式的构造和求导.[正则表达式][数据结构][排序] 第二次作业:含三角函数因子的复杂多项式的构造.求导和化简.[递归 ...
- GoLang之方法与接口
GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...
- python 静态 封装 继承 mro 接口 super
1.静态属性 静态方法 类方法 #!/usr/bin/python env # encoding: utf-8 # 静态属性 静态方法 class Room: tag = 168 def __ini ...
随机推荐
- Python中tab键自动补全功能的配置
新手学习Python的时候,如何没有tab键补全功能,我感觉那将是一个噩梦,对于我们这种菜鸟来说,刚接触python,对一切都不了解,还好有前辈们的指导,学习一下,并记录下来,还没有学习这个功能小伙伴 ...
- iOS: UUID and SSKeyChain
需要加入SSKeyChain文件 传送门:SSKeyChain // // UniqueIDCreater.h // Housemart // // Created by Haozhen Li on ...
- CString TCHAR互相转换
CString->TCHAR*的转化可以用函数GetBuffer() // 原型:LPTSTR GetBuffer( int nMinBufLength ); CString str(_T(&q ...
- IOS -执行时 (消息传递 )
一 函数调用概述 Objective-C不支持多重继承(同Java和Smalltalk),而C++语言支持多重继承. Objective-C是动态绑定,它的类库比C++要easy操作. Ob ...
- 利用Sharepoint 创建轻量型应用之基本功能配置!
博客同步课程.假设你想跟着视频学习,请跟着例如以下视频: http://edu.csdn.net/course/detail/2097 1. 点击安装程序,出现的界面先期安装完毕准备工具,准备工具 ...
- Linux中下载、解压、安装文件(转)
原文地址:http://www.cnblogs.com/red-code/p/5539399.html 一.将解压包发送到linux服务器上: 1.在windos上下载好压缩包文件后,通过winscp ...
- 关于直播学习笔记-004-nginx-rtmp、srs、vlc、obs
1.采集端:OBS RTMP推流地址:rtmp://192.168.198.21:1935/live 流密钥:livestream(任意-但播放地址与此一致) 2.播放端:nginx-rtmp-win ...
- mySQL数据库二:命令行的使用
在做整理的时候,上一篇刚开始只是简单的做了个数据类型的开头,在这里简单说一下mySQL的使用以及它的命令行 1.准备工作 有一个好的开发工具可以几何倍数的增加我们的工作效率,所以,工具是必不可少的,首 ...
- 在js中通过call或者apply实现继承
通过call或者apply可以实现函数里面this的改变,利用这一特点,可以实现继承 代码如下所示: /*父类*/ function Parent(add,net,no,teacher) { this ...
- 【转】Go Channels
转自: http://kdf5000.com/2017/07/16/Go-Channels/ Golang使用Groutine和channels实现了CSP(Communicating Sequent ...