# [译] part 28: golang 的面向对象 -- 多态

·  阅读 1392

Go 中的多态性是在接口的帮助下实现的。正如我们已经讨论过的，接口可以在 Go 中隐式实现。如果类型为接口中声明的所有方法提供定义，则类型实现接口。让我们看看在接口的帮助下如何在 Go 中实现多态性。

## 使用接口实现多态

``````type Income interface {
calculate() int
source() string
}

``````type FixedBilling struct {
projectName string
biddedAmount int
}

`FixedBilling`项目有两个字段`projectName`，表示项目名称，`biddedAmount`是组织为项目出价的金额。

`TimeAndMaterial`结构表示按时间计算收益的项目

``````type TimeAndMaterial struct {
projectName string
noOfHours  int
hourlyRate int
}

`TimeAndMaterial`结构有三个字段`projectName``noOfHours``hourlyRate`

``````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 organisation = \$%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)
}

``````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 organisation = \$%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)
}

Run in playground

``````Income From Project 1 = \$5000
Income From Project 2 = \$10000
Income From Project 3 = \$4000
Net income of organisation = \$19000

## 在上述代码中添加新的收入来源

``````type Advertisement struct {
CPC        int
noOfClicks int
}

return a.CPC * a.noOfClicks
}

}

`Advertisement`类型有三个字段：`adName``CPC`（每次点击费用）和`noOfClicks`（点击次数）。广告的总收入是`CPC``noOfClicks`的乘积。

``````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}
calculateNetIncome(incomeStreams)
}

``````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
}

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
}

return a.CPC * a.noOfClicks
}

}
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 organisation = \$%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}
calculateNetIncome(incomeStreams)
}

Run in playground

``````Income From Project 1 = \$5000
Income From Project 2 = \$10000
Income From Project 3 = \$4000
Income From Banner Ad = \$1000
Income From Popup Ad = \$3750
Net income of organisation = \$23750