swift初始化
class INIT: NSObject {
// 一个结构体的初始化
// 1、存储属性的初始化
struct Fahrenheit {
var temperature :Double
init(){
temperature = 32.0
}
}
func createTemperature(){
var myTemperature = Fahrenheit()
println("The default temperature is \(myTemperature.temperature)° Fahrenheit")
}
// 2、自定义初始化(Customizing Initialization)
struct Celeius {
var temperatureInCelsius: Double = 0.0
init(fromFahrenheit fahrenheit: Double){
temperatureInCelsius = (fahrenheit - 32.0) / 1.8
}
init(fromKevi keviValue: Double){
temperatureInCelsius = keviValue - 273.15
}
}
func createCeleius(){
var celeius = Celeius(fromFahrenheit: 25.0)
println("The tempeture is \(celeius.temperatureInCelsius)")
}
// 3. 定义一个RGB结构体
swift初始化
最新推荐文章于 2024-08-31 12:57:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)