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 ...
随机推荐
- e661. 确定图像中是否有透明像素
// This method returns true if the specified image has transparent pixels public static boolean hasA ...
- iOS 开发系列:CoreData Object 变成 Fault 的一种方式
@quote: 近来一直与 CoreData 打交道.这是一个架构庞大.学习曲线比較陡峭的 iOS 组件,每次遇到问题都会对其有新的认识. 这次就仅仅讲一点,关于错误认知 Object(NSManag ...
- Session超时问题(AOP 过滤器)
public class TimeoutAttribute : ActionFilterAttribute { public override void OnActionExecuting(Actio ...
- BIEE物理业务层编辑之后发布路径
在BI 业务逻辑层编辑之后,需要发布,地址是http://pc201411260149:7001/em/, IP/em 在business intelligence 页面,点击部署,然后选择文件发布
- 谈谈django里的Contex和RequestContext---向模板里添加全局变量
一直很想仔细研究一下,我在django模板里,可以直接访问变量user, request之类的变量,哪里来的,到底都有哪些?这会儿周五,我有空来仔细看看代码. 模拟一下需求: 我们做一个在线商城,需要 ...
- 在工程名.h头文件中写public:
class CaccessimageApp : public CWinApp { public: _ConnectionPtr m_pConnection; CaccessimageApp(); // ...
- 第三章 Spring.Net 环境准备和搭建
在前面一章我们介绍了依赖注入,控制反转的概念.接下来我们来真正动手搭建一下Spring.Net的环境,看一下Spring.Net 中的控制反转和依赖注入是什么样子. 3.1 Spring.Net 下 ...
- oracle启动报ORA-03113;
[案例] 在重启数据库过程中: SQL> startup ORACLE instance started. Total System Global Area 1.0489E+10 bytes F ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
使用maven打包的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compil ...
- 制作Windows U盘镜像
目的:制作windows server 2008 U盘镜像 需要的共具: 1.一个格式为FAT并且至少4G的U盘, 2.UltraISO软件, 3.一个windows server 2008 ISO文 ...