class Foo{
[string]$SomePram
Foo([string]$somePram){
$this.SomePram = $somePram
}
[string]GetMessage(){
return $null
}
[void]WriteMessage(){
Write-Host($this.GetMessage())
}
}
class Bar : Foo{
Bar([string]$somePram): base($somePram){
}
[string]GetMessage(){
return ("{0} Success" -f $this.SomePram)
}
}
class Bar2 : Foo{
Bar2([string]$somePram): base($somePram){
}
[string]GetMessage(){
return ("{0} Success" -f $this.SomePram)
}
}
[Foo[]]$foos = @([Bar]::new("Bar"), [Bar2]::new("Bar2"))
foreach($foo in $foos){
$foo.WriteMessage()
}
Wednesday, 21 October 2015
Polymorphism Powershell v5 Classes
I have been doing a lot of work lately with Microsoft's Desired State Configuration and creating custom class resources. As the code is getting more complicated I came accross an instance where I wanted to extend a class to have different implementations put into a list. After a quick google found no results I thought I would give it a go myself and sure enough it was just like c#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment