go 面向对象 -ag捕鱼王app官网

go 面向对象 - go中的多态

作者:迹忆客 最近更新:2023/01/08 浏览次数:

go 面向对象中的多态性是在接口(interface)的帮助下实现的。 正如我们已经讨论过的,go 中的接口是隐式实现的。 如果某一类型为接口中声明的所有方法提供定义,则认为该类型实现了这个接口。 让我们看看如何借助接口在 go 中实现多态性。

使用接口实现多态

任何为接口中声明的所有方法提供定义的类型都被称为隐式实现了该接口。 当我们稍后讨论一个多态性的例子时,这一点会更加清楚。

接口类型的变量可以保存实现接口的任何值。 接口的这个属性用于在 go 中实现多态。

让我们借助一个计算公司净收入的程序来理解 go 中的多态性。 为简单起见,让我们假设这个虚构的组织有来自两种项目的收入,即。 固定的账单和时间材料。 该组织的净收入由这些项目的收入总和计算。 为了让本教程理解起来更简单,我们假设货币是美元,我们不会处理美分。 它将使用 int 表示。

我们首先定义一个接口 income

type income interface {  
    calculate() int
    source() string
}

上面定义的 income 接口包含两个方法: calculate() 计算并返回来源的收入; source() 返回来源的名称。

接下来,让我们为项目类型定义一个结构体 fixedbilling

type fixedbilling struct {  
    projectname string
    biddedamount int
}

fixedbilling 项目有两个字段: projectname 表示项目名称;biddedamount 表示组织为项目投标的金额。

timeandmaterial 结构体将代表时间材料类型的项目。

type timeandmaterial struct {  
    projectname string
    noofhours  int
    hourlyrate int
}

timeandmaterial 结构体具有三个字段,名称为 projectnamenoofhourshourlyrate

下一步是定义这些结构类型的方法,用于计算和返回实际收入和收入来源。

func (fb fixedbilling) calculate() int {  
    return fb.biddedamount
}
func (fb fixedbilling) source() string {  
    return fb.projectname
}
func (tm timeandmaterial) calculate() int {  
    return tm.noofhours * tm.hourlyrate
}
func (tm timeandmaterial) source() string {  
    return tm.projectname
}

对于 fixedbilling 项目,收入只是项目的投标金额。 因此我们从 fixedbilling 类型的 calculate() 方法中返回它。

对于 timeandmaterial 项目,收入是 noofhourshourlyrate 的乘积。 该值是从具有接收器类型 timeandmaterial 的 calculate() 方法返回的。

我们从 source() 方法返回项目名称作为收入来源。

由于 fixedbilling 和 timeandmaterial 结构体都提供了 income 接口的 calculate() 和 source() 方法的定义,因此这两个结构体都实现了 income 接口。

让我们声明 calculatenetincome 函数,它将计算和打印总收入。

func calculatenetincome(ic []income) {  
    var netincome int = 0
    for _, income := range ic {
        fmt.printf("income from %s = $%d\n", income.source(), income.calculate())
        netincome  = income.calculate()
    }
    fmt.printf("net income of organization = $%d", netincome)
}

上面的 calculatenetincome 函数接受 income 接口切片作为参数。 它通过迭代切片并对其每个项目调用 calculate() 方法来计算总收入。 它还通过调用 source() 方法显示收入来源。 根据 income 接口的具体类型,将调用不同的 calculate() 和 source() 方法。 因此我们在calculatenetincome 函数中实现了多态性。

将来,如果组织增加了一种新的收入来源,该功能仍然可以正确计算总收入,而无需更改一行代码:)。

程序中唯一剩下的部分是 main 函数。

func main() {  
    project1 := fixedbilling{projectname: "project 1", biddedamount: 5000}
    project2 := fixedbilling{projectname: "project 2", biddedamount: 10000}
    project3 := timeandmaterial{projectname: "project 3", noofhours: 160, hourlyrate: 25}
    incomestreams := []income{project1, project2, project3}
    calculatenetincome(incomestreams)
}

在上面的 main 函数中,我们创建了三个项目变量,两个是 fixedbilling 类型,一个是 timeandmaterial 类型。 接下来,我们使用这 3 个项目创建一个类型为 income 的切片。 由于这些项目中的每一个都实现了 income 接口,因此可以将所有三个项目添加到 income 类型的切片中。 最后,我们调用 calculatenetincome 函数并将这个切片作为参数传递。 它将显示各种收入来源及其收入。

下面是完整的程序。

package main
import (  
    "fmt"
)
type income interface {  
    calculate() int
    source() string
}
type fixedbilling struct {  
    projectname string
    biddedamount int
}
type timeandmaterial struct {  
    projectname string
    noofhours  int
    hourlyrate int
}
func (fb fixedbilling) calculate() int {  
    return fb.biddedamount
}
func (fb fixedbilling) source() string {  
    return fb.projectname
}
func (tm timeandmaterial) calculate() int {  
    return tm.noofhours * tm.hourlyrate
}
func (tm timeandmaterial) source() string {  
    return tm.projectname
}
func calculatenetincome(ic []income) {  
    var netincome int = 0
    for _, income := range ic {
        fmt.printf("income from %s = $%d\n", income.source(), income.calculate())
        netincome  = income.calculate()
    }
    fmt.printf("net income of organization = $%d", netincome)
}
func main() {  
    project1 := fixedbilling{projectname: "project 1", biddedamount: 5000}
    project2 := fixedbilling{projectname: "project 2", biddedamount: 10000}
    project3 := timeandmaterial{projectname: "project 3", noofhours: 160, hourlyrate: 25}
    incomestreams := []income{project1, project2, project3}
    calculatenetincome(incomestreams)
}

运行上面程序,结果如下

go 多态性

为上述程序添加新的收入来源

假设该公司通过广告找到了新的收入来源。 让我们看看添加这个新的收入流并计算总收入而不对 calculatenetincome 函数进行任何更改是多么简单。 由于多态性,这成为可能。

让我们首先定义 advertisement 以及 advertisement 上的 calculate()source() 方法。

type advertisement struct {  
    adname     string
    cpc        int
    noofclicks int
}
func (a advertisement) calculate() int {  
    return a.cpc * a.noofclicks
}
func (a advertisement) source() string {  
    return a.adname
}

advertisement 包含三个字段 adname、cpc(每次点击费用)和 noofclicks(点击次数)。 广告的总收入是 cpc 和 noofclicks 的乘积。

让我们稍微修改 main 函数以包含这个新的收入来源。

func main() {  
    project1 := fixedbilling{projectname: "project 1", biddedamount: 5000}
    project2 := fixedbilling{projectname: "project 2", biddedamount: 10000}
    project3 := timeandmaterial{projectname: "project 3", noofhours: 160, hourlyrate: 25}
    bannerad := advertisement{adname: "banner ad", cpc: 2, noofclicks: 500}
    popupad := advertisement{adname: "popup ad", cpc: 5, noofclicks: 750}
    incomestreams := []income{project1, project2, project3, bannerad, popupad}
    calculatenetincome(incomestreams)
}

我们创建了两个广告,即 bannerad 和 popupad。 income 切片包括我们刚刚创建的两个广告。

下面是添加广告后的完整程序。

package main
import (  
    "fmt"
)
type income interface {  
    calculate() int
    source() string
}
type fixedbilling struct {  
    projectname  string
    biddedamount int
}
type timeandmaterial struct {  
    projectname string
    noofhours   int
    hourlyrate  int
}
type advertisement struct {  
    adname     string
    cpc        int
    noofclicks int
}
func (fb fixedbilling) calculate() int {  
    return fb.biddedamount
}
func (fb fixedbilling) source() string {  
    return fb.projectname
}
func (tm timeandmaterial) calculate() int {  
    return tm.noofhours * tm.hourlyrate
}
func (tm timeandmaterial) source() string {  
    return tm.projectname
}
func (a advertisement) calculate() int {  
    return a.cpc * a.noofclicks
}
func (a advertisement) source() string {  
    return a.adname
}
func calculatenetincome(ic []income) {  
    var netincome int = 0
    for _, income := range ic {
        fmt.printf("income from %s = $%d\n", income.source(), income.calculate())
        netincome  = income.calculate()
    }
    fmt.printf("net income of organization = $%d", netincome)
}
func main() {  
    project1 := fixedbilling{projectname: "project 1", biddedamount: 5000}
    project2 := fixedbilling{projectname: "project 2", biddedamount: 10000}
    project3 := timeandmaterial{projectname: "project 3", noofhours: 160, hourlyrate: 25}
    bannerad := advertisement{adname: "banner ad", cpc: 2, noofclicks: 500}
    popupad := advertisement{adname: "popup ad", cpc: 5, noofclicks: 750}
    incomestreams := []income{project1, project2, project3, bannerad, popupad}
    calculatenetincome(incomestreams)
}

运行上面的程序,结果如下

go 多态性示例

可能已经注意到,尽管我们添加了新的收入流,但我们并未对 calculatenetincome 函数进行任何更改。 它因为多态而起作用。 由于新的 advertisement 类型也实现了 income 接口,我们可以将其添加到incomestreams 切片中。 calculatenetincome 函数也无需任何更改即可工作,因为它能够调用 advertisement 类型的 calculate() 和 source() 方法。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

c# 中的 goto 语句

发布时间:2024/02/02 浏览次数:184 分类:编程语言

本教程演示了如何在 c# 中使用 goto 以及何时使用它会有所帮助本教程将演示如何在 c# 中使用 goto 语法,并提供一些代码中的实际使用示例。

发布时间:2023/12/20 浏览次数:197 分类:python

本文为你提供了 python 中是否存在 goto 语句的答案。本文为你提供了 python 中是否存在 goto 语句的答案。基本上,python 不支持 goto 语句。

java goto

发布时间:2023/08/07 浏览次数:83 分类:java

与其他编程语言不同,java 没有 goto。 相反,java 包含关键字 label。关键字 label 的作用是改变程序的流程,根据指定的条件跳转到程序的另一段。

发布时间:2023/07/03 浏览次数:70 分类:python

本篇文章将介绍如何在 python 中实现多态性。python 中的多态性 理解这一点的最佳方法是使用 len() 函数。 对于不同的对象,该函数有不同的解释。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图