go 接口多态实现

204 阅读1分钟
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} type yx struct {    masterName string    house int    money 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 (man yx) source() string {    return man.masterName} func (man yx) calculate() int {    return man.house * man.money} 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}    bannerAd := Advertisement{adName: "Banner Ad", CPC: 2, noOfClicks: 500}    popupAd := Advertisement{adName: "Popup Ad", CPC: 5, noOfClicks: 750}    jarvis := yx{masterName:"jarvis", house:8, money:10}    incomeStreams := []Income{project1, project2, project3, bannerAd, popupAd, jarvis}    calculateNetIncome(incomeStreams)}