Examples/04/JeffHicks_StarShipModule.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
#requires -version 5.0 #The original script/Module is available here --> https://github.com/jdhitsolutions/myStarShip #All rights go to Jeff Hicks #demonstrate PowerShell classes in v5 using inheritance #This is a Non-DSC resource example. #region Enums Enum ShipClass { Shuttle Clipper Frigate Transport XWing Cruiser Destroyer Battlestar Dreadnought } Enum ShipSpeed { Stopped Impulse Sublight Translight } Enum Cloak { Absent Present } #endregion #region general class definition Class mystarshIp { #region properties [ValidateNotNullorEmpty()] [string]$Name [ValidateNotNullorEmpty()] [Shipclass]$ShipClass [ValidateNotNullorEmpty()] [ShipSpeed]$Speed [ValidateRange(1,1000)] [int]$Crew = 1 [boolean]$Shields = $False [ValidateRange(0,20)] [int]$Torpedos = 0 [string]$Captain = $env:USERNAME [Cloak]$CloakingDevice #These are hidden properties hidden [string]$Transponder = [guid]::NewGuid().Guid hidden [datetime]$ManufacturingDate = (Get-Date).AddYears(245) #endregion #region methods #returns the new crew complement [int]AddCrew([int]$Number) { $this.Crew+= $Number return $this.Crew } [timespan]GetAge() { $timespan = (Get-Date).AddYears(250) - $this.ManufacturingDate return $timespan } [string]GetAge([switch]$AsString) { $timespan = (Get-Date).AddYears(250) - $this.ManufacturingDate return $timespan.ToString() } #return True or False if operation was successful [Boolean]RaiseShields() { if (-Not $this.shields) { $this.shields = $True #must use Return keyword Return $True } else { Write-Warning "Are you paying attention Captain $($this.captain)? The shields are already up." Return $False } } #close RaiseShields method [Boolean]LowerShields() { if ($this.shields) { $this.shields = $False Return $True } else { Write-Warning "Are you paying attention Captain $($this.captain)? The shields are already down." Return $False } } #close RaiseShields method [void]OpenCommunication () { 1..7 | foreach { $f = Get-Random -Minimum 500 -Maximum 1200 $d = Get-Random -Minimum 90 -Maximum 125 [console]::Beep($f,$d) } } #close OpenCommunication #region using overloads [void]Fire() { if ($this.Torpedos -gt 0 ) { Write-Host "Fire!" -ForegroundColor Red -BackgroundColor Yellow $this.Torpedos-=1 } else { Write-Warning "There's nothing left to fire." } } #close first Fire method [void]Fire([int]$Count) { 1..$count | foreach { if ($this.Torpedos -ge $_) { Write-Host "Fire $_ !" -ForegroundColor Red -BackgroundColor Yellow $this.Torpedos-=1 Start-Sleep -Milliseconds 200 } else { Write-Warning "There's nothing left to fire." #bail out Break } } } #close second Fire method #endregion #endregion #region constructor #I have 2 ways of constructing an instance of this class MyStarship() {} MyStarShip([string]$Name,[ShipClass]$ShipClass) { $this.Name = $Name $this.ShipClass = $ShipClass $this.Speed = "Stopped" } #endregion } #close MyStarship #endregion Class Cruiser : MyStarship { #define different property defaults [ShipClass]$ShipClass = [shipclass]::Cruiser [int]$Torpedos = 10 [int]$Crew = 200 #custom constructor Cruiser([string]$Name) { $this.name = $Name } } Class Dreadnought : MyStarShip { [ShipClass]$ShipClass = [shipclass]::Dreadnought [int]$Torpedos = 20 [int]$Crew = 900 [Cloak]$CloakingDevice = [cloak]::Present #add a new property [ValidateRange(1,100)] [int]$FluxCapacitor = 50 #add a new method [boolean]TestFluxCapacitor() { if ($this.FluxCapacitor -ge 50) { Return $True } else { Return $False } } Dreadnought([string]$Name) { $this.name = $Name } } RETURN #region fun with the class #look at constructor [cruiser]::new $a = [Cruiser]::new("Barney") $a $a | Get-Member $a.Speed = "impulse" $b = [dreadnought]::new("Cerebus") $b | Get-Member $b $b.TestFluxCapacitor() $b.fire(5) $b | format-table $c = [MyStarship]::new("Ad Astra","Clipper") $c.Captain = 'Jason' $c $a,$b,$c | format-table -AutoSize #add some type information Update-TypeData -TypeName MyStarship -DefaultDisplayPropertySet "Name","ShipClass","Captain","Crew","Speed" $c $a $a,$b,$c #You could also come up with custom format extensions. Care to try? #endregion |