-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.Interface.go
More file actions
58 lines (46 loc) · 976 Bytes
/
Copy path12.Interface.go
File metadata and controls
58 lines (46 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import "fmt"
type SalaryCalculator interface {
CalculateSalary() int
}
type staff1 struct {
empID int
basicPay int
}
type staff2 struct {
empID int
basicPay int
bonus int
}
func (s staff1) CalculateSalary() int {
return s.basicPay
}
func (s staff2) CalculateSalary() int {
return s.basicPay + s.bonus
}
func totalExpense(s []SalaryCalculator) int {
//类型断言
for _, value := range s {
switch value.(type) {
case staff1:
println("员工1正在计算中")
case staff2:
println("员工2计算中")
default:
println("未知错误")
}
}
expense := 0
for _, value := range s {
expense = expense + value.CalculateSalary()
}
return expense
}
func main() {
s1 := staff1{empID: 1001, basicPay: 100}
s2 := staff1{empID: 1002, basicPay: 100}
s3 := staff2{empID: 1003, basicPay: 100, bonus: 100}
salCal := []SalaryCalculator{s1, s2, s3}
expense := totalExpense(salCal)
fmt.Println("总支出:", expense)
}