Golang实现接口编程

楚天乐 3747 0 条

介绍

接口是面向对象编程一种重要技术手段。一个接口中包含一组标准操作的签名,任意class只要实现了这个接口的方法,就可以被当做这个接口的子类型来调用。因为这个特性,我们就可以实现功能组件的即插即用,随意替换。

举个cache的例子,这里用java来写:

public class Main {

    public static void main(String[] args){

        Cache cache;

        // 输出:Redis缓存内容
        cache = new RedisCache();
        System.out.println(cache.get("key"));

        // 输出:File缓存内容
        cache = new FileCache();
        System.out.println(cache.get("key"));

    }

}

// 缓存接口
interface Cache {
    Object get(String key);
    void set(String key, Object value);
    void delete(String key);
}

// 基于redis的缓存
class RedisCache implements Cache{
    public Object get(String key){
        return new String("Redis缓存内容");
    }

    public void set(String Sey, Object value){
    }

    public void delete(String key){
    }
}

// 基于file的缓存
class FileCache implements Cache{
    public Object get(String key){
        return new String("File缓存内容");
    }
    public void set(String key, Object value){
    }

    public void delete(String key){
    }
}

上面这个例子中,我们使用Cache cache对象来保存缓存组件对象,一个类型兼容多种不同实现方案。实际应用中我们可以根据配置文件来决定cache到底是保存FileCache,还是RedisCache。这就是即插即用了。

golang中interface怎么用

首先我们定义一个接口Cache

package main

import "fmt"

// 定义接口
type Cache interface{
    get(key string)
    set(key string, value interface{})
    delete(key string)
}

// 在redis cache类上实现接口
type RedisCache struct{}
func (redis RedisCache) get(key string){
    fmt.Println("这里是redis cache")
}
func (redis RedisCache) set(key string, value interface{}){
}
func (redis RedisCache) delete(key string){
}

// 在file cache类上实现接口
type FileCache struct{}
func (redis FileCache) get(key string){
    fmt.Println("这里是file cache")
}
func (redis FileCache) set(key string, value interface{}){
}
func (redis FileCache) delete(key string){
}

func main(){
    var cache Cache

    // 创建redis cache
    cache = new(RedisCache)
    cache.get("keyt")

    // 创建file cache
    cache = new(FileCache)
    cache.get("keyt")
}

运行结果

这里是redis cache
这里是file cache

Process finished with exit code 0

语法千差万别,思路还是一样的。只是golang这个class的搞法我还真的有点不适应

如果说给定接口我不全部实现呢?测试一下把RedisCache和FileCache的set,delete方法删掉。

package main

import "fmt"

// 定义接口
type Cache interface{
    get(key string)
    set(key string, value interface{})
    delete(key string)
}

// 在redis cache类上实现接口
type RedisCache struct{}
func (redis RedisCache) get(key string){
    fmt.Println("这里是redis cache")
}

// 在file cache类上实现接口
type FileCache struct{}
func (redis FileCache) get(key string){
    fmt.Println("这里是file cache")
}

func main(){
    var cache Cache

    // 创建redis cache
    cache = new(RedisCache)
    cache.get("keyt")

    // 创建file cache
    cache = new(FileCache)
    cache.get("keyt")
}

运行报错

# command-line-arguments
chapter1\interface1.go:28:8: cannot use new(RedisCache) (type *RedisCache) as type Cache in assignment:
    *RedisCache does not implement Cache (missing delete method)
chapter1\interface1.go:32:8: cannot use new(FileCache) (type *FileCache) as type Cache in assignment:
    *FileCache does not implement Cache (missing delete method)

虽然实现接口函数的时候,没有像java那样明确引用接口,但是再用new创建时候,编译器会发现类型不匹配,实现不完整。

不过瘾,再来一个例子吧

你妈要揍你,她有一万种方法。可选择用手打你,用擀面杖打你。来我们来搞个接口实现

package main

import "fmt"

// 打接口
type Kick interface{
    kick()
    //love()
}

// 手打你
type HandKick struct{
}
func (hand HandKick) kick(){
    fmt.Println("你妈在用手打你。。。")
}

// 擀面杖打你
type RodKick struct{
}
func (rod RodKick) kick(){
    fmt.Println("你妈在用擀面杖打你。。。")
}

func main(){
    var kick Kick

    kick = new (HandKick)
    kick.kick()

    kick = new (RodKick)
    kick.kick()
}

输出

你妈在用手打你。。。
你妈在用擀面杖打你。。。

Process finished with exit code 0


发表我的评论
昵称 (必填)
邮箱 (必填)
网址
执行时间: 1711694491098.8 毫秒