/ClassDefinition.ps1 Secret
Last active
August 29, 2015 14:04
-
-
Save Jaykul/5e838aa38e46a93e91e1 to your computer and use it in GitHub Desktop.
Playing with classes in PowerShell 5 (July Preview)
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
class Person1 { | |
[string]$FirstName = "" | |
[string]$LastName = "" | |
[int]$Age = 0 | |
} | |
class Person2 { | |
[string]$FirstName = "" | |
[string]$LastName = "" | |
[int]$Age = 0 | |
# If you write a constructor, you loose the ability to cast hashtables | |
def Person2 { | |
# The [Parameter] block doesn't work here. | |
param([String]$Name, [Parameter(Mandatory=$false)][Int]$YearsOld) | |
$this.FirstName, $this.LastName = $Name -split " " | |
$this.Age = $YearsOld | |
} | |
} | |
# namespace Users { | |
enum Role { | |
Guest | |
User | |
Admin | |
Owner | |
} | |
class Person3 { | |
# You can't assign values to static members yet | |
static $DefaultRole = 0 | |
[string]$FirstName = $null | |
[string]$LastName = $null | |
[int]$Age = $null | |
$Role = $null | |
# Static constructors, aren't. | |
static def Person3 { | |
[Role]$this::DefaultRole = [Role]::Guest | |
} | |
def Person3 { | |
param([String]$Name, [Int]$Age) | |
$this.FirstName, $this.LastName = $Name -split " " | |
# Currently, you can't use a parameter name the same as a property | |
Write-Host "$Age $($this.Age)" | |
$this.Age = $Age | |
if($this::DefaultRole -eq $null) { | |
Write-Warning "Static property doesn't have value yet, assigning one at random" | |
[Role]$this::DefaultRole = [Enum]::GetValues([Role]) | Get-Random | |
} | |
[Role]$this.Role = $this::DefaultRole | |
} | |
[string] def FullName { | |
return $this.FirstName + " " + $this.LastName | |
} | |
} | |
#} | |
class Person4 { | |
[string]$FirstName = $null | |
[string]$LastName = $null | |
[int]$Age = $null | |
# You can have constructor overloads | |
def Person4 { | |
param([String]$Name, [Int]$YearsOld) | |
$this.FirstName, $this.LastName = $Name -split " " | |
$this.Age = $YearsOld | |
} | |
def Person4 { | |
param([String]$FirstName, [String]$LastName, [Int]$YearsOld) | |
$this.FirstName = $FirstName | |
$this.LastName = $LastName | |
$this.Age = $YearsOld | |
} | |
def Person4 { | |
param() | |
} | |
} | |
[Person2]::new("Joel Bennett", 70) | |
$P = [Person3]::new("Bill Buckley", 42) | |
$P | |
$P.FullName() | |
[Person4]@{FirstName="Bob";LastName="Newhart";Age="21"} | |
[Enum]::GetValues([Role]) | |
class MyTestClass | |
{ | |
[int] $MyIntProperty = 1 | |
[bool] def MyMethod | |
{ | |
return $true | |
} | |
static [int] $MyStaticIntProperty = 5 | |
static [int] def MyStaticMethod | |
{ | |
return [MyTestClass]::MyStaticIntProperty | |
} | |
} | |
$object = New-Object MyTestClass | |
$object.MyIntProperty = 5 | |
$object.MyMethod() | |
$object.GetType() | |
# nothing | |
[MyTestClass]::MyStaticIntProperty | |
[MyTestClass]::MyStaticIntProperty = 10 | |
[MyTestClass]::MyStaticIntProperty | |
# wtf? | |
[MyTestClass]::MyStaticMethod() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment