Last active
August 29, 2015 14:06
-
-
Save guitarrapc/e17fa71083847c9ba78d to your computer and use it in GitHub Desktop.
PowerShell Class Constructor Sample
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
######################## | |
# Constructor Call check | |
######################## | |
class Hoge | |
{ | |
Hoge() | |
{ | |
Write-Host "constuctor called" | |
} | |
} | |
[Hoge]::new() | |
<# | |
constuctor called | |
#> | |
######################## | |
# Initialization | |
######################## | |
class MyClass | |
{ | |
[string]$name | |
[int]$age | |
MyClass() | |
{ | |
$name = "" | |
$age = 0 | |
} | |
} | |
[MyClass]::new() | |
<# | |
name age | |
---- --- | |
0 | |
#> | |
######################## | |
# This not supported | |
######################## | |
class MyClass | |
{ | |
[string]$name; | |
MyClass([string]$name) | |
{ | |
$name = $name #this not yet usable | |
} | |
} | |
[MyClass]::new("hoge") | |
<# | |
name | |
---- | |
#> | |
######################## | |
# Constructor can overload | |
######################## | |
class MyClass | |
{ | |
[string]$name | |
[int]$age | |
MyClass() | |
{ | |
$name = "" | |
$age = 0 | |
} | |
MyClass([string]$hoge, [int]$fuga) | |
{ | |
$name = $hoge | |
$age = $fuga | |
} | |
} | |
[MyClass]::new | |
<# | |
OverloadDefinitions | |
------------------- | |
MyClass, powershell, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null new() | |
MyClass, powershell, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null new(string hoge, int fuga) | |
#> | |
[MyClass]::new("hoge", 10) | |
<# | |
name age | |
---- --- | |
hoge 10 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"constuctor called"