有了域名就可以做网站了吗,泰州网站制作企业,肥乡县建设局网站,广州h5网站制作公司Go 中的多态性是通过接口实现的。正如我们已经讨论过的#xff0c;接口在 Go 中是隐式实现的。如果类型为接口中声明的所有方法提供定义#xff0c;则该类型实现了接口。让我们看看 Go 中如何借助接口实现多态性。
使用接口的多态性
任何为接口的所有方法提供定义的类型都被…Go 中的多态性是通过接口实现的。正如我们已经讨论过的接口在 Go 中是隐式实现的。如果类型为接口中声明的所有方法提供定义则该类型实现了接口。让我们看看 Go 中如何借助接口实现多态性。
使用接口的多态性
任何为接口的所有方法提供定义的类型都被称为隐式实现该接口。当我们稍后讨论多态性的示例时这一点将会更加清楚。
接口类型的变量可以保存实现该接口的任何值。Go 中利用接口的这一特性来实现多态性。
让我们借助一个计算组织净收入的程序来了解 Go 中的多态性。为了简单起见我们假设这个虚构的组织有来自两种项目的收入即。固定计费、时间和材料。该组织的净收入是根据这些项目的收入之和计算的。为了使本教程简单我们假设货币是美元。它将使用 来表示int。
我们首先定义一个接口Income。
type Income interface { calculate() intsource() string
}上面定义的接口Income包含两个方法calculate()分别计算并返回来源的收入和source()返回来源的名称。
接下来让我们为FixedBilling项目类型定义一个结构体。
type FixedBilling struct { projectName stringbiddedAmount int
}该FixedBilling项目有两个字段projectName、biddedAmount分别代表项目名称和组织对该项目投标的金额。
该TimeAndMaterial结构将代表时间和材料类型的项目。
type TimeAndMaterial struct { projectName stringnoOfHours inthourlyRate 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
}就固定计费而言FixedBilling收入只是项目的投标金额。该值是从接收者类型为FixedBilling的calculate()方法返回的
就时间和材料类型的项目收入是noOfHours和hourlyRate的乘积。该值是从接收者类型为TimeAndMaterial 的calculate()方法返回的
我们返回项目名称作为该方法的收入来源source()。
由于 FixedBilling和TimeAndMaterial结构都提供了接口的方法的定义因此两个结构都实现了该接口。calculate()和source()
让我们声明一个calculateNetIncome计算并打印总收入的函数。
func calculateNetIncome(ic []Income) { var netincome int 0for _, 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()方法。这样我们就实现了函数的多态性
将来如果组织添加了一种新的收入来源该函数仍然可以正确计算总收入而无需更改任何代码:)。
程序中唯一剩下的部分是主函数。
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的切片中。最后我们调用calculateNetIncome函数并将此切片作为参数传递。它将显示各种收入来源以及从中获得的收入。
这是完整的程序供您参考。
package mainimport ( fmt
)type Income interface { calculate() intsource() string
}type FixedBilling struct { projectName stringbiddedAmount int
}type TimeAndMaterial struct { projectName stringnoOfHours inthourlyRate 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 0for _, 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)
}Run program in playground
该程序将输出
Income From Project 1 $5000
Income From Project 2 $10000
Income From Project 3 $4000
Net income of organization $19000 在上述计划中添加新的收入来源
假设该组织通过广告找到了新的收入来源。让我们看看添加这个新的收入流并计算总收入是多么简单而无需对函数进行任何更改calculateNetIncome。由于多态性这成为可能。
让我们首先定义Advertisement类型以及该calculate()和source()的方法。
type Advertisement struct { adName stringCPC intnoOfClicks 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。该incomeStreams切片包含我们刚刚创建的两个广告。
这是添加广告后的完整程序。
package mainimport ( fmt
)type Income interface { calculate() intsource() string
}type FixedBilling struct { projectName stringbiddedAmount int
}type TimeAndMaterial struct { projectName stringnoOfHours inthourlyRate int
}type Advertisement struct { adName stringCPC intnoOfClicks 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 0for _, 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)
}Run program 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 organization $23750 calculateNetIncome您可能已经注意到尽管我们添加了新的收入来源但我们并未对该功能进行任何更改。它只是因为多态性而起作用。由于新Advertisement类型也实现了该Income接口因此我们能够将其添加到incomeStreams切片中。该calculateNetIncome函数无需任何更改即可运行因为它能够调用该类型的calculate()和source()方法。
本教程到此结束。祝你有美好的一天。