struct Fahrenheit {
var temperature: Double
init() {
temperature = 32.0
}
}
var f = Fahrenheit()
print("The default temperature is \(f.temperature)° Fahrenheit")
// Prints "The default temperature is 32.0° Fahrenheit"
struct Fahrenheit {
var temperature = 32.0
}
struct Celsius {
var temperatureInCelsius: Double
init(fromFahrenheit fahrenheit: Double) {
temperatureInCelsius = (fahrenheit - 32.0) / 1.8
}
init(fromKelvin kelvin: Double) {
temperatureInCelsius = kelvin - 273.15
}
}
let boilingPointOfWater = Celsius(fromFahrenheit: 212.0)
// boilingPointOfWater.temperatureInCelsius is 100.0
let freezingPointOfWater = Celsius(fromKelvin: 273.15)
// freezingPointOfWater.temperatureInCelsius is 0.0
struct Size {
var width = 0.0, height = 0.0
}
let twoByTwo = Size(width: 2.0, height: 2.0)
class Size {
var width:Double, height:Double
init(w:Double, h:Double) {
width = w
height = h
}
}
let twoByTwo = Size(w: 2.0, h: 2.0)
class SomeClass {
required init() {
// initializer akan diimplementasikan di sini
}
}
class SomeSubclass: SomeClass {
required init() {
// implementasi subkelas dari initializer required ada di sini
}
}