Skip to content

Instantly share code, notes, and snippets.

@vel-inet
Last active March 22, 2024 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vel-inet/da0ce0a2670e29abc2347e6efc3fde02 to your computer and use it in GitHub Desktop.
Save vel-inet/da0ce0a2670e29abc2347e6efc3fde02 to your computer and use it in GitHub Desktop.
идиоматичный полиморфизм с функциями-конструкторами вместо скриптблоков
$PI = [Math]::PI
# два конструктора
function new-square {
param ($name, $side)
@{
Name = $name
Perimeter = ($side * 4)
Area = ($side * $side)
}
}
function new-circle {
param ($name, $radius)
@{
Name = $name
Perimeter = ($PI * 2 * $radius)
Area = ($PI * $radius * $radius)
}
}
# массив из сконструированных хэштаблиц
$examples = (new-square "sq" 3.0), (new-circle "ci" 2.0)
$examples | ForEach-Object {
Write-Host ("{0} {1:N2} {2:N2}" -f $_.Name, $_.Perimeter, $_.Area)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment