BikeGarage, CustomStringConvertible
This commit is contained in:
parent
64da46dc27
commit
adea84b66b
4 changed files with 49 additions and 7 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
|
|
@ -1,11 +1,25 @@
|
||||||
// https://carrascomolina.com
|
// https://carrascomolina.com
|
||||||
import Playgrounds
|
import Playgrounds
|
||||||
|
import Foundation
|
||||||
|
|
||||||
protocol Vehicle {
|
protocol Vehicle {
|
||||||
// name ist read-only
|
// name ist read-only
|
||||||
var name: String {get}
|
var name: String {get}
|
||||||
var color: String {get set}
|
var color: String {get set}
|
||||||
}
|
}
|
||||||
|
protocol Garage {
|
||||||
|
associatedtype V: Vehicle
|
||||||
|
func park(vehicle: V)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BikeGarage: Garage {
|
||||||
|
|
||||||
|
|
||||||
|
func park(vehicle: Bike){
|
||||||
|
vehicle.emptyBasket()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Train: Vehicle {
|
struct Train: Vehicle {
|
||||||
var name: String
|
var name: String
|
||||||
|
|
@ -24,10 +38,13 @@ struct ICE: Vehicle {
|
||||||
var color = "white"
|
var color = "white"
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Bike: Vehicle {
|
struct Bike: Vehicle, CustomStringConvertible {
|
||||||
var name: String
|
var name: String
|
||||||
var color: String
|
var color: String
|
||||||
// newValue ist ein special keyword
|
// newValue ist ein special keyword
|
||||||
|
func emptyBasket(){
|
||||||
|
print(description + " korb ausgekippt", Date())
|
||||||
|
}
|
||||||
var description: String {
|
var description: String {
|
||||||
set { name = newValue }
|
set { name = newValue }
|
||||||
get {"A bike called \(name) and it is \(color)." }}
|
get {"A bike called \(name) and it is \(color)." }}
|
||||||
|
|
@ -35,15 +52,16 @@ struct Bike: Vehicle {
|
||||||
}
|
}
|
||||||
|
|
||||||
#Playground {
|
#Playground {
|
||||||
var greeting = "Hallo Playground"
|
// var greeting = "Hallo Playground"
|
||||||
var bike = Bike.trek
|
var bike = Bike.trek
|
||||||
var train = Train(name:"ICE")
|
var train = Train(name:"ICE")
|
||||||
bike.description
|
// bike.description
|
||||||
bike.description = "Mein Rad"
|
// bike.description = "Mein Rad"
|
||||||
bike.description
|
// bike.description
|
||||||
|
|
||||||
train
|
train
|
||||||
train.delayed(minutes: 23)
|
train.delayed(minutes: 23)
|
||||||
|
BikeGarage().park(vehicle: bike)
|
||||||
|
print( bike)
|
||||||
// write code here
|
// write code here
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Bucket
|
||||||
|
uuid = "543AEB13-B7EA-4C8C-9F73-C23287B3971E"
|
||||||
|
type = "1"
|
||||||
|
version = "2.0">
|
||||||
|
<Breakpoints>
|
||||||
|
<BreakpointProxy
|
||||||
|
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
|
||||||
|
<BreakpointContent
|
||||||
|
uuid = "60F4FEF4-4F42-4C29-B795-B68DFA0CB921"
|
||||||
|
shouldBeEnabled = "No"
|
||||||
|
ignoreCount = "0"
|
||||||
|
continueAfterRunningActions = "No"
|
||||||
|
filePath = "Playground.swift"
|
||||||
|
startingColumnNumber = "9223372036854775807"
|
||||||
|
endingColumnNumber = "9223372036854775807"
|
||||||
|
startingLineNumber = "36"
|
||||||
|
endingLineNumber = "36"
|
||||||
|
landmarkName = "ICE"
|
||||||
|
landmarkType = "14">
|
||||||
|
</BreakpointContent>
|
||||||
|
</BreakpointProxy>
|
||||||
|
</Breakpoints>
|
||||||
|
</Bucket>
|
||||||
Loading…
Reference in a new issue