8
0
Skriptentwickung/examples/PowerShell/Klassenbeispiel mit Konstruktor.ps1

31 lines
670 B
PowerShell

class Auto {
[string]$Marke
[string]$Modell
[int] $Baujahr
Auto ([string]$marke, [string]$modell, [int]$baujahr) {
$this.Marke = $marke
$this.Modell = $modell
$this.Baujahr = $baujahr
}
[string] GetAutoInfo() {
return "$($this.Marke) $($this.Modell) aus dem Jahr $($this.Baujahr)"
}
[int] GetAutoAlter() {
$aktuellesJahr = (Get-Date).Year
return $aktuellesJahr - $this.Baujahr
}
}
# aus dem gleichen Ordner:
#. .\Auto.ps1
$meinAuto = [Auto]::new("Volkswagen", "Golf", 2020)
$meinAuto.GetAutoInfo() # "Volkswagen Golf aus dem Jahr 2020"
$meinAuto.GetAutoAlter() # z. B. 5