golang面向接口

我不是女神ヾ 2023-07-10 08:15 88阅读 0赞

go语言是一门面向接口的编程语言

在这里插入图片描述

1.接口的定义和实现

工程目录

1.1接口定义

接口是一个或多个方法的集合

  1. /* @FileName: main.go @Time : 2020/3/1 20:08 @Author : Cocktail_py */
  2. package main
  3. import (
  4. "awesomeProject/dbinterface/mysql"
  5. "fmt"
  6. )
  7. //定义一个数据库接口 具有读写功能
  8. type Database interface {
  9. Read() map[string]string
  10. Write(form map[string]string) string
  11. }
  12. func main() {
  13. var db Database
  14. db = &mysql.Database{ }
  15. //fmt.Println(db)
  16. fmt.Println(db.Read())
  17. db.Write(map[string]string{ "name": "Cocktail_py", "course": "golang"})
  18. fmt.Println(db.Read())
  19. }
1.2接口实现

接口的实现是隐式的
只要实现接口里的方法

  1. /* @FileName: database.go @Time : 2020/3/1 20:08 @Author : Cocktail_py */
  2. package mysql
  3. type Database struct {
  4. Content map[string]string
  5. }
  6. //实现Read功能
  7. func (db *Database) Read() map[string]string {
  8. return db.Content
  9. }
  10. //实现Write功能
  11. func (db *Database) Write(form map[string]string) string {
  12. db.Content = form
  13. return "ok"
  14. }
2.接口的值类型

interface{} 支持任何类型
接口变量包含:实现者的类型,实现者的值

  1. /* @FileName: queue3.go @Time : 2020/3/1 21:08 @Author : Cocktail_py */
  2. package queue3
  3. //interface{} 支持任何类型
  4. type Queue []interface{ }
  5. func (q *Queue)Push(v interface{ }) {
  6. *q= append(*q,v)
  7. }
  8. func (q *Queue)Pop()interface{ } {
  9. head := (*q)[0]
  10. *q = (*q)[1:]
  11. //Type Switch
  12. //接口变量包含:实现者的类型,实现者的值
  13. switch head.(type) {
  14. case int:
  15. return head.(int)
  16. case string:
  17. return head.(string)
  18. default:
  19. panic("Not correct value")
  20. }
  21. return head
  22. }
  23. func (q * Queue) IsEmpty() bool {
  24. return len(*q) == 0
  25. }
3.接口的组合

在这里插入图片描述

  1. /* @FileName: main.go @Time : 2020/3/1 20:08 @Author : Cocktail_py */
  2. package main
  3. import (
  4. "awesomeProject/dbinterface/dbqueue"
  5. )
  6. //定义一个Database接口
  7. type Database interface {
  8. Read() map[string]string
  9. Write(form map[string]string) string
  10. }
  11. //定义一个Database接口
  12. type Queue interface {
  13. Pop() int
  14. Push(int)
  15. }
  16. //DBQueue为Database接口与Database接口的组合
  17. type DBQueue interface {
  18. Database
  19. Queue
  20. }
  21. func getValue(dbq DBQueue) {
  22. dbq.Read()
  23. }
  24. func main() {
  25. db := &dbqueue.DbQueue{ }
  26. getValue(db)
  27. }

实现dbqueue所有功能

  1. /* @FileName: dbqueue.go @Time : 2020/3/1 20:08 @Author : Cocktail_py */
  2. package dbqueue
  3. type DbQueue struct {
  4. Content map[string]string
  5. }
  6. func (d DbQueue) Read() map[string]string {
  7. panic("implement me")
  8. }
  9. func (d DbQueue) Write(form map[string]string) string {
  10. panic("implement me")
  11. }
  12. func (d DbQueue) Pop() int {
  13. panic("implement me")
  14. }
  15. func (d DbQueue) Push(int) {
  16. panic("implement me")
  17. }

发表评论

表情:
评论列表 (有 0 条评论,88人围观)

还没有评论,来说两句吧...

相关阅读

    相关 golang面向对象编程

    面向对象编程 对于面向对象编程的支持Go 语言设计得非常简洁而优雅。因为, Go语言并没有沿袭传统面向对象编程中的诸多概念,比如继承(不支持继承,尽管匿名字段的内存布局和

    相关 golang 接口interface 09

     1.接口会自动和拥有相同方法的结构体关联 / 只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。 如果一个变量含有了多个int

    相关 golang接口声明

    接口本身是调用方和实现方需要遵守的一种协议,大家按照统一的方法命名参数类型和数量来协调逻辑处理的过程。 golang使用组合实现对象特性的描述。对象的内部使用结构体

    相关 Golang 接口深入讨论

    一 点睛 1 Golang 接口中不能有任何变量。 2 一个接口(比如 A 接口)可以继承多个别的接口(比如 B、C 接口),这时如果要实现 A 接口,也必须将 B、C