1 回答

TA贡献1842条经验 获得超12个赞
如果地图中的函数(即HOLD()、INB()、 等)不需要比 StatModel 接口提供的更多的模型访问权限,那么您的需求应该可以通过更改这些函数来接收 StatModel 而不是 NoaggRow 来实现:
func HOLD(s StatModel) float64 {
...
}
那么你要添加的AcsiRow函数可以有相同的签名,statCount和Count可以改写如下:
func statCount(model StatModel, f func(StatModel) float64) float64 {
countedStat := f(model)
return countedStat
}
func Count(model StatModel, name string) float64 {
m := map[string]func(StatModel) float64 {
"HOLD" : HOLD,
"INB" : INB,
"AHT" : AHT,
"RING" : RING,
"TALK" : TALK,
"ACW" : ACW,
"OCC" : OCC,
// Map AcsiModel functions here, let's call them ACSIn here
// (you mentioned that the names would be different, so
// I assume they don't get in the way of the functions
// above):
"ACSI1": ACSI1,
"ACSI2": ACSI2,
"ACSI3": ACSI3,
}
countedStat := statCount(model, m[name])
return countedStat
}
免责声明:这只是基于我在帖子中看到的内容的一个想法。我对 Revel 一无所知,因此也许我可能错过了一些由 Revel 上下文引起的重要细节,这些细节会阻止实施这种方法。
根据评论更新:
使用类型的断言,以使HOLD,OCC等功能,以访问结构特定的属性,像这样:
func HOLD (s StatModel) float64 {
noagg, ok := s.(NoaggRow) // typecast from StateModel to NoaggRow
if !ok {
// Looks like the caller did not pass a matching model/name pair into Count().
// Error handling...
}
attr := noagg.AttributeSpecificToNoaggRow
...
}
这在运行时可能看起来有点危险,但如果调用者总是将匹配的模型/名称值对传递给 Count()(我假设它这样做了),则类型转换应该是安全的。
- 1 回答
- 0 关注
- 174 浏览
添加回答
举报