我一直在研究封装的概念,并找到了一些关于这个主题的不错的线索,比如this one和this one。但是我还没有找到一个答案来回答我的问题。我将用Swift中的一个例子。
假设您有一个RoadTrip类型的对象:
class RoadTrip() {
private var duration: Double
private var totalMiles: Double
private var gallonsOfFuel: Double
var averageMilesPerGallon: Double
}
现在假设应用程序将计算
averageMilesPerGallon
这是唯一的公共属性:func calculateAverageMilePerGallon() -> Double {
let mpg = totalMiles / gallonsOfFuel
return mpg
}
每加仑平均英里数的计算应该是执行并更新其
averageMilesPerGallon
的RoadTrip对象的私有方法,还是可以由另一个方法在单独的实用程序类中执行,然后使用将设置该值的mutator方法更新RoadTrip对象的averageMilesPerGallon
属性?编辑:这是我的一个类,包含我的应用程序的基本计算。我是根据我在斯坦福大学iTunes课程中学到的知识,这样做的,但我开始想,在我的案例中,我应该把大部分内容转移到我的生活课上:
infix operator ^^ { }
func ^^ (radix: Double, power: Double) -> Double {
return Double(pow(Double(radix), Double(power)))
}
class CalculatorBrain: NSObject {
var weightLifted: Double?
var repetitions: Double?
var oneRepMax: Double?
let dataManager = LiftEventDataManager()
func calculateOneRepMax(weightLifted: Double, repetitions: Int ) -> Double {
let preferredFormulaID = UserDefaultsManager.sharedInstance.preferredFormula!
let formulas = dataManager.fetchSelectableFormulas()
let formulaName = formulas[preferredFormulaID].formulaName
switch formulaName {
case "Epley":
oneRepMax = weightLifted * (1 + Double(repetitions)/30.0)
return oneRepMax!
case "Baechle":
oneRepMax = weightLifted * (36/(37 - Double(repetitions)))
return oneRepMax!
case "Brzychi":
oneRepMax = weightLifted * ( 1 + ( 0.033 * Double(repetitions)))
return oneRepMax!
case "Lander":
oneRepMax = 100 * weightLifted / (101.3 - (2.67123 * Double(repetitions)))
return oneRepMax!
case "Lombardi":
oneRepMax = weightLifted * (Double(repetitions) ^^ 0.10)
return oneRepMax!
case "Mayhew":
oneRepMax = 100 * weightLifted / (52.2 + (41.9 * (2.71828 ^^ (-0.055 * Double(repetitions)))))
return oneRepMax!
case "O'Conner":
oneRepMax = weightLifted * (1 + 0.025 * Double(repetitions))
return oneRepMax!
default:
return 0.0
}
}
private func calculatePercentOfWeight(maxWeight: Double, percent: Double) -> Double {
return maxWeight * percent
}
func calculateWeightPercentages(maxWeight: String) -> [Int: Double] {
let weightPercentages = [1.0, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25]
var percentages = [Int: Double]()
for percent in weightPercentages {
let integerPercent = Int(percent * 100)
percentages[integerPercent] = calculatePercentOfWeight(Double(maxWeight)!, percent: percent)
}
return percentages
}
func convertBetweenUnits(fromUnit: Int, toUnit: Int, value: Double) -> Double {
let units = dataManager.fetchUnits()
let from = units[fromUnit].conversionRatio as Double
let to = units[toUnit].conversionRatio as Double
let result = Double(value * to / from)
return result
}
}
最佳答案
我认为这是计算属性的理想用例:
class RoadTrip {
private let duration: Double
private let totalMiles: Double
private let gallonsOfFuel: Double
private var averageMilesPerGallon: Double {
return totalMiles / gallonsOfFuel
}
}
关于swift - 正确使用封装,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41912700/