49 lines
1,014 B
Swift
49 lines
1,014 B
Swift
// https://carrascomolina.com
|
|
import Playgrounds
|
|
|
|
protocol Vehicle {
|
|
// name ist read-only
|
|
var name: String {get}
|
|
var color: String {get set}
|
|
}
|
|
|
|
struct Train: Vehicle {
|
|
var name: String
|
|
var color = "white"
|
|
var delay = 0 // nicht im protocol
|
|
func delayed(minutes: Int) -> Train {
|
|
var train = self
|
|
train.delay = minutes
|
|
return train
|
|
}
|
|
|
|
}
|
|
|
|
struct ICE: Vehicle {
|
|
var name = "ICE"
|
|
var color = "white"
|
|
}
|
|
|
|
struct Bike: Vehicle {
|
|
var name: String
|
|
var color: String
|
|
// newValue ist ein special keyword
|
|
var description: String {
|
|
set { name = newValue }
|
|
get {"A bike called \(name) and it is \(color)." }}
|
|
static let trek = Bike(name: "Trek", color: "red" )
|
|
}
|
|
|
|
#Playground {
|
|
var greeting = "Hallo Playground"
|
|
var bike = Bike.trek
|
|
var train = Train(name:"ICE")
|
|
bike.description
|
|
bike.description = "Mein Rad"
|
|
bike.description
|
|
|
|
train
|
|
train.delayed(minutes: 23)
|
|
|
|
// write code here
|
|
}
|