go的interface示例实现多态范式
2018-11-09 21:35
393 查看
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/83868186
看程序:
[code]package main import "fmt" type BaseIntf interface { Process() } type Msg1 struct { req int rsp int } func (p *Msg1) Process() { fmt.Println("process 1") } type Msg2 struct { req int rsp int } func (p *Msg2) Process() { fmt.Println("process 2") } func main() { m1 := new(Msg1) m1.Process() m2 := new(Msg2) m2.Process() }
变一下:
[code]package main import "fmt" type BaseIntf interface { Process() } func Run(proc BaseIntf) { fmt.Println("run") proc.Process() } type Msg1 struct { req int rsp int } func (p *Msg1) Process() { fmt.Println("process 1") } type Msg2 struct { req int rsp int } func (p *Msg2) Process() { fmt.Println("process 2") } func main() { m1 := new(Msg1) Run(m1) m2 := new(Msg2) Run(m2) }
这种风格的代码,见了很多次了。
不多说。
阅读更多
相关文章推荐
- php通过interface实现简单多态
- Go如何实现HTTP请求限流示例
- Go 多态功能实现
- go语言如何实现类似c++中的多态功能
- JAVA知识回顾-5(Interface与abstract类的介绍、Static class与not static class的区别、java多态的实现原理、Thread与Runable)
- Go 多态功能实现
- 面向对象思考及go实现继承与多态
- go 快排实现示例
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:避免限定于一种特定的列表实现(多态迭代)
- Polynom-Interface通过接口实现多态
- 再看go的interface代码示例
- 阅读go项目源码,解决无法快速找到interface有哪些struct实现问题
- 面向对象多态的实现(抽象类Abstract和接口Interface的用法区别)
- go语言实现聊天服务器的示例代码
- JavaScript使用prototype原型实现的封装继承多态示例
- 俺使用的C语言面向对象范式(第四节:多态的实现 与 总结)
- Go实现短url项目的方法示例
- Go语言实现类似c++中的多态功能实例
- 到底是使用abstract method还是interface在实现多态时
- 1行Go代码实现反向代理的示例