BikeGarage, CustomStringConvertible

This commit is contained in:
Ingo Rohlf 2025-10-20 14:48:30 +02:00
parent 64da46dc27
commit adea84b66b
4 changed files with 49 additions and 7 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -1,11 +1,25 @@
// https://carrascomolina.com
import Playgrounds
import Foundation
protocol Vehicle {
// name ist read-only
var name: String {get}
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 {
var name: String
@ -24,10 +38,13 @@ struct ICE: Vehicle {
var color = "white"
}
struct Bike: Vehicle {
struct Bike: Vehicle, CustomStringConvertible {
var name: String
var color: String
// newValue ist ein special keyword
func emptyBasket(){
print(description + " korb ausgekippt", Date())
}
var description: String {
set { name = newValue }
get {"A bike called \(name) and it is \(color)." }}
@ -35,15 +52,16 @@ struct Bike: Vehicle {
}
#Playground {
var greeting = "Hallo Playground"
// var greeting = "Hallo Playground"
var bike = Bike.trek
var train = Train(name:"ICE")
bike.description
bike.description = "Mein Rad"
bike.description
// bike.description
// bike.description = "Mein Rad"
// bike.description
train
train.delayed(minutes: 23)
BikeGarage().park(vehicle: bike)
print( bike)
// write code here
}

View file

@ -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>