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

什么是接口?

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

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

高级语言C#
定义

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

接口示例
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();
   }
}
高级语言C++ 
定义

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

接口示例
#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;
  }
};

如何使用接口?

面向对象编程(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接口? - 知乎


#转载请注明出处!
快来制作你的简历吧 ,请猛戳这里→点击在线制作
宝塔服务器面板,一键全能部署及管理,送你3188元礼包。请猛戳这里→点我领取

本文标题:《Go实战(篇二)使用接口构建对象》作者:xuanzhe
原文链接:https://blog.xuanzhe.club/?id=67
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

发表评论:
验证码

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

你好,朋友

真是美好的一天!

访客信息

  • IP: 18.224.67.149
  • 地点: United StatesOhioDublin

标签列表

站点信息

  • 文章总数:69
  • 页面总数:2
  • 分类总数:19
  • 标签总数:34
  • 评论总数:7
  • 浏览总数:147010
您好,欢迎到访网站!
忘记密码

网站分类

文章归档

歌曲 - 歌手
0:00