-
-
Save vel-inet/da0ce0a2670e29abc2347e6efc3fde02 to your computer and use it in GitHub Desktop.
идиоматичный полиморфизм с функциями-конструкторами вместо скриптблоков
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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