从OOP的角度看Golang
资料来源
Golang中的概念
Golang中引入了在Golang中独有的struct 和 interface,其意义和传统语言中的不同。
概念清单
Golang | Classic OOP |
---|---|
struct | class with fields, only non-virtual methods |
interface | class without fields, only virtual methods |
embedding | multiple inheritance AND composition |
receiver | implicit this parameter |
struct 是一个class但是没有virtual方法
struct 是一个class但是没有virtual方法,比如说:
type Rectangle struct {
Name string
Width, Height float64
}
func(r Rectangle) Area() float64 {
return r.Width + r.Height
}
那么伪代码如下:
class Rectangle
field Name: string
field Width: float64
field Height: float64
method Area() //non-virtual
return this.Width * this.Height
构造器
- 对于每一个基本类型,默认使用0来填充,如果你没有显示的指定其初始值,那么就是0
- 没有指定的类的构造器,只是对每一个类的实例,都有一个通用的构造器。使用类似于JSON格式的字面值常量来构造
function construct ( class, literal)
helper function assignFields ( object, literal) //recursive
if type-of object is "object"
if literal has field-names
for each field in object.fields
if literal has-field field.name
assignFields(field.value, literal.fields[field.name].value) //recurse
else
//literal without names, assign by position
for n=0 to object.fields.length
assignFields(object.fields[n].value, literal.fields[n].value) //recurse
else
//atom type
set object.value = literal.value
// generic constructor main body
var classInstance = new class
assignFields(classInstance, literal)
return classInstance
对于Golang的构造器
使用
package main
import . "fmt"
type Rectangle struct {
Name string
Width, Height float64
}
func main() {
var a Rectangle
var b = Rectangle{"I'm b.", 10, 20}
var c = Rectangle{Height: 12, Width: 14}
Println(a)
Println(b)
Println(c)
}
Golang中的嵌入类似于OOP中的没有virtual 方法的多继承
将一个结构体嵌入到另一个结构体,就好像是对非虚成员的多继承。让我们看看一个例子:
type base struct {
a string
b int
}
type derived {
base //嵌入
d int
a float32 //隐藏base中的a字段
}
func main() {
var x derived;
fmt.Printf("%T", x.a) // x.a float32
fmt.Printf("%T", x.base.a) //=> x.
}
当base结构体嵌入到derived结构体中,那么base结构体中的字段和方法都可以在derived结构体中都可用。但是base内部的字段可以被derived中的字段shadowed
。在内部,有一个名称叫base 类型为base的字段被创建,。如果将base嵌入到了derived,那么在derived类型可以直接访问base的字段。
Shadowing表示在derived中定义另一个与base 字段和方法相同名字的字段和方法,那么就会存在shadowinig这种情况。一但base中的字段被derived中的shadow,那么可以通过使用base名称来访问隐藏的字段。所有base中的字段,都可以通过隐藏字段的名称base来访问。需要注意的事情:
It is important to note that all inherited methods are called on the hidden-field-struct. It means that a base method cannot see or know about derived methods or fields. Everything is non-virtual.
当和结构体和嵌入这种概念打交道的时候,我们应该知道所有的东西都是在编译期间静态绑定的,所以的引用解析都是在静态编译期间。
When working with structs and embedding, everything is STATICALLY LINKED. All references are resolved at compile time.
多层嵌入
type NamedObj struct {
Name string
}
type Shape struct {
NameObj
color string
isRegular bool
}
type Point struct {
x, y float64
}
type Rectangle struct {
NameObj
Shape
center Point
Width float64
Height float64
}
func main() {
var aRect = Rectange {
NameObj{"name1"},
Shape{NameObj{"name2"}, 0, true},
Point{0, 0},
20,
2.5
}
fmt.Println(aRect.Name)
fmt.Println(aRect.Shape)
fmt.Println(aRect.Shape.Name)
}
等效的伪代码:
class NamedObj
field Name: string
class Shape
inherits NamedObj
field color: int32
field isRegular: bool
class Rectangle
inherits NamedObj
inherits Shape
field center: Point
field Width: float64
field Height: float64
在var aRect Rectangle
中,
- aRect.Name and aRect.NamedObj.Name 指的是同意对象
- aRect.color and aRect.Shape.color 指的是同一个东西
- aRect.name and aRect.NamedObj.name 指的是同一个字段, 但是 aRect.NamedObj.name 和 aRect.Shape.NamedObj.name 是不同的字段
- aRect.NamedObj and aRect.Shape.NamedObj 是相同的类型,但是对象不同
方法隐藏
由于所有的golang-struct都是non-virtual的,所以你不可以覆盖这些方法。(当然接口是可以的)比如在NameObj中定义了一个名字叫做show的方法,但是呢,你在struct Rectangle中也定义了其他的方法,那么
Rectangle/show()将会隐藏NameObj/show方法。正如基类的字段一样,你也可以通过使用基类的名称来访问基类被隐藏的方法。
type base struct {
a string
b int
}
func (this base) xyz(){
fmt.Println("xyz, a is ", this.a);
}
// method display
func (this base) display() {
fmt.Println("base, a is:", this.a)
}
type derived struct {
base // embedding
d int
a float32 //-SHADOWED
}
// method display
func(this derived) display() {
fmt.Println("derived a is ", this.a)
}
func main() {
var a derived = derived{base{"base-a", 10}, 20, 2.5}
a.display() // 2.5
a.base.diplay() // base, a is : base-a
a.xyz(); // call base.xyz(), xyz, a is : base-a
}
Golang 方法和接受者
Golang的 struct-method,和类的non-virtual方法类似,但是在如下地方不同:
- struct-method在struct外面定义
- 既然起在struct body外面定义,那么在方法中有了一个名字叫做接受者
- 对于这个接受者的名字,自己可以取一个简单的,一般是struct类型的第一个单词
type NameObj struct {
Name string
}
func(n NameObj) show(){
fmt.Println(n.Name)
}
struct 和 interface
Golang中的interface是一个没有字段的,只有虚方法。interface的设计是为了补充struct。在Golang中
**Structs: ** classes, with fields, ALL NON-VIRTUAL methods **Interfaces: ** classes, with NO fields, ALL VIRTUAL methods
通过严格限定struct是没有virtual的方法,interface是包含所有virtual的方法,通过嵌入来将两者结合起来,从而实现多态。
Interface
Golang interface 是一个没有字段的类,但是包含所有是virtual的函数。考虑到这种定义,那么你能够用interface干的事情:
- 声明一个变量的类型为 type interface
- 实现该interface,在一个struct中实现所有该interface定义的方法
- 嵌入到另一个interface
空接口
在Golang中空接口是没有任何字段和没有任何方法,所以任何struct和基本类型都实现了interface{},这意味着var x interface{}
可以容纳任何类型的值。那么我们可以使用interface{}
来干什么事情呢?本来什么事情都不可以干,但是我们可以使用type switch
,type assertion
或反射
使用struct embedding
当你使用struct embedding,那么你就是要创建一个multi-root 体系,通过多继承。你要记住的是,所有的结构体方法都是non-virtual的。这就是为什么struct永远要比使用接口嵌入要快。因为没有接口涉及的时候,所有的函数的调用都是在编译期间就决定了。
从OOP的角度看Golang的更多相关文章
- 从JDK源码角度看Object
Java的Object是所有其他类的父类,从继承的层次来看它就是最顶层根,所以它也是唯一一个没有父类的类.它包含了对象常用的一些方法,比如getClass.hashCode.equals.clone. ...
- Android IOS WebRTC 音视频开发总结(四八)-- 从商业和技术的角度看视频行业的机会
本文主要从不同角度介绍视频行业的机会,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,欢迎关注个人微信公众号blacker ----------------------------- ...
- 【阿里云产品公测】以开发者角度看ACE服务『ACE应用构建指南』
作者:阿里云用户mr_wid ,z)NKt# @I6A9do 如果感觉该评测对您有所帮助, 欢迎投票给本文: UO<claV RsfTUb)< 投票标题: 28.[阿里云 ...
- [置顶] 从引爆点的角度看360随身wifi的发展
从引爆点的角度看360随身wifi的发展 不到一个月的时间,随身wifi预定量就数百万.它的引爆点在哪里,为什么相同的产品这么多它却能火起来,通过对随身wifi的了解和我知识层面分析,主要是因为随身w ...
- 站在Java的角度看LinkedList
站在Java的角度看,玩队列不就是玩对象引用对象嘛! public class LinkedList<E> implements List<E>, Deque<E> ...
- 从源码的角度看 React JS 中批量更新 State 的策略(下)
这篇文章我们继续从源码的角度学习 React JS 中的批量更新 State 的策略,供我们继续深入学习研究 React 之用. 前置文章列表 深入理解 React JS 中的 setState 从源 ...
- 从线程模型的角度看Netty的高性能
转载:Netty(二) 从线程模型的角度看 Netty 为什么是高性能的? 传统 IO 在 Netty 以及 NIO 出现之前,我们写 IO 应用其实用的都是用 java.io.* 下所提供的包. 比 ...
- INDEX--从数据存放的角度看索引2
在上次<INDEX--从数据存放的角度看索引>中,我们说到"唯一非聚集索引"和“非唯一非聚集索引”在存储上有一个明显的差别:唯一非聚集索引的非叶子节点上不会包含RID的 ...
- 从JDK源码角度看Short
概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...
随机推荐
- DHT协议C++实现过程中各种问题
---恢复内容开始--- 博主是个菜鸟,什么地方写的不对或者理解有误的地方还请指正出来. DHT协议是BT协议中的一部分,也是一个辅助性的协议.HTTP协议中用 来定位资源也就是html文本是用URL ...
- juqery 正则表达式的使用
juqery 正则表达式的使用test() 方法用于检测一个字符串是否匹配某个模式.js原生的方法 reg="/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8} ...
- Mac Pro 日历增强工具 Itsycal
Mac 自带的日历工具一点也不好用,好在 Itsycal 弥补了这个缺陷,效果图如下: 下载地址:https://www.mowglii.com/itsycal/,下载解压后,把 Itsycal.ap ...
- css负边距之详解
自从1998年CSS2作为推荐以来,表格的使用渐渐退去,成为历史.正因为此,从那以后CSS布局成为了优雅代码的代名词. 对于所有设计师使用过的CSS概念,负边距作为最少讨论到的定位方式要记上一功.这就 ...
- mac php环境启动
mac 环境下,用brew安装php相关环境启动命令 说明 这里php,mysql,nginx都是用brew安装,安装目录默认,在Cellar下面 php-fpm 带配置重启 /*注意权限,加 sud ...
- bzoj violet系列 (2708~2725)
cbh大爷说:写博客不能弃坑. orz cbh 那我就来更新博客了. violet这个系列的题好神啊……出题人好劲啊…… ……怎么最近都在理性愉悦啊…… 另外bzoj400题纪念~ 2708: [Vi ...
- Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...
- Qtp常见问题
(1)Qtp不能识别对象 插件加载错误 IE内未加载BHOManager加载项
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- iOS 编译时的警告导致无法通过编译
今天编译react native的代码,发现了2个警告,但是系统却当做错误,不能编译成功,查看了一下编译选项,看到了如下配置: 注意到这个-Werror 了吗? 就是这个标志导致系统把所有的 警告都当 ...