0
点赞
收藏
分享

微信扫一扫

Go实战(篇二)使用接口构建对象

扒皮狼 2022-08-16 阅读 92

什么是接口?

面向对象编程(OOP)中三个基本特征分别是封装,继承,多态。在 Go 语言中封装和继承是通过 struct 来实现的,而多态则是通过接口(interface)来实现的。

接口是一种重要的实现运行时多态的方式,也是一种协议。它规范了传入的对象所必须具备的某些特征,从而保证在调用时既不会发生错误又不需要提前检查。虽然继承也能起到相同的效果,但是很多场景下使用继承会导致逻辑关系混乱,不利于抽象。


高级语言

C#

C++ 

Go

定义

接口定义协定。 实现该协定的任何 class 或 struct 必须提供接口中定义的成员的实现。 从 C# 8.0 开始,接口可为成员定义默认实现。 它还可以定义 static 成员,以便提供常见功能的单个实现。 从 C# 11 开始,接口可以定义 static abstract 或 static virtual 成员来声明实现类型必须提供声明的成员。 通常,static virtual 方法声明实现必须定义一组重载运算符

所谓的接口,即将内部实现细节封装起来,外部用户用过预留的接口可以使用接口的功能而不需要知晓内部具体细节。C++中,通过类实现面向对象的编程,而在基类中只给出纯虚函数的声明,然后在派生类中实现纯虚函数的具体定义的方式实现接口,不同派生类实现接口的方式也不尽相同,从而实现多态。

在 Go 语言中接口包含两种含义:它既是方法的集合, 同时还是一种类型。在Go 语言中是隐式实现的,对于一个具体的类型,不需要声明它实现了哪些接口,只需要提供接口所必需的方法。在 Go 语言的类型系统中有一个核心概念: 我们不应该根据类型可以容纳哪种数据而是应该根据类型可以执行哪种操作来设计抽象类型.


接口示例

interface ISampleInterface
{
void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}

static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();

// Call the member.
obj.SampleMethod();
}
}

#include <iostream>
using namespace std;

class Shape
{
public:
// 提供接口框架的纯虚函数
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// 派生类
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};

package main

import (
"fmt"
)

type Human interface {
Say()
}

type man struct {
}
func (m *man) Say() {
fmt.Println("I'm a man")
}

type women struct {
}
func (w *women) Say() {
fmt.Println("I'm a women")
}

func main(){

m := new(man)
m.Say()
w := new(women)
w.Say()

}

如何使用接口?

在面向对象编程(OOP)中,我们用不同类型的变量描述一个对象,使用关键字“new”创建对象,随后使用该变量引用该对象的公开方法。

在Go语言中,也有类似的方法可以用接口实现。声明一个human结构体,并构造Human接口实现Get、Set和Say方法,代码如下:

package main

import (
"fmt"
)

//创建Human接口
//这里实现的接口类似OOP中的“public”关键字
type Human interface {
//这里用类进行描述
//Set一个human类,并返回该类型。如果没有返回,这个类的属性全部为空
Set(name string, sex string, age int) human
//Get方法,获取name属性
Name() string
//Get方法,获取sex属性
Sex() string
//Get方法,获取age属性
Age() int
//Say方法,输出对象的值
Say(h human)
}

type human struct {
name string
sex string
age int
}

func (h human) Set(name string, sex string, age int) human {
h.name = name
h.sex = sex
h.age = age
return h
}

func (h human) Name() string {
return h.name
}

func (h human) Sex() string {
return h.sex
}

func (h human) Age() int {
return h.age
}

func (h human) Say(hu human) {
fmt.Printf("I'm a %d-year-old %s,my name is %s.\n", hu.age, hu.sex, hu.name)
}

func main() {

//创建接口,可以看成new出一个无参对象
//因为接口中不存在参数,所以这里使用的是一个human类型,而不是对象
method := Human(new(human))
hu := method.Set("Bob", "boy", 25)
fmt.Printf("I'm a %d-year-old %s,my name is %s.\n", hu.age, hu.sex, hu.name)
hu.Say(method.Set("Alice", "girl", 24))
fmt.Printf("I'm a %d-year-old %s,my name is %s.\n", hu.age, hu.sex, hu.name)

}

/*
输出的结果为:
I'm a 25-year-old boy,my name is Bob.
I'm a 24-year-old girl,my name is Alice.
I'm a 25-year-old boy,my name is Bob.

*/

写在最后的话

以上就是Go语言操接口的介绍了,暂时先写这么多,后面也会进行完善。

你知道的越多,你不知道的越多,人才们的 【三连】 就是我创作的最大动力,我们下期见!

注:如果本篇博客有任何错误和建议,欢迎人才们留言,你快说句话啊!


参考:

  1. ​​接口 - C# 参考​​
  2. ​​C++接口的定义与实现_一只特立独行的程序猿的博客-CSDN博客_c++接口开发​​
  3. ​​如何优雅的使用Go接口? - 知乎​​


原文链接:​​Go实战(篇二)使用接口构建对象_Go_轩哲驿站​​

举报

相关推荐

0 条评论