Har mitt i allt börjat bygga PowerShell classer
Mina tre första är Continent, Country och Person.
Tänk på att detta är min första utgåva av dessa classer och det kan bli så att jag kommer justera dem lite mera innan jag är helt nöjd.
#Class Person, object with Person class Person { #Hidden properties hidden [String] $Firstname #Firstname for person hidden [String] $Lastname #Lastname for person #Constructor Person(){} #Constructor Person([String] $Firstname, [String] $Lastname){ $this.Firstname = $Firstname $this.Lastname = $Lastname } #Write to cli method [void] WriteToCli(){ Write-Host "##### DATA to CLI - Start #####" Write-Host "Firstname:" $this.Firstname Write-Host "Lastname:" $this.Lastname Write-Host "##### DATA to CLI - End #####" } #Get method [String] getFirstname(){ return $this.Firstname } #Get method [String] getLastname(){ return $this.Lastname } #Get method [String] getDisplayname(){ $str = $this.Firstname + " " + $this.Lastname return $str } #Set method [void] setFirstname([String] $Firstname){ $this.Firstname = $Firstname } #Set method [void] setLastname([String] $Lastname){ $this.Lastname = $Lastname } #compare method [boolean] compare([Person] $person){ $bool = $true if($this.getFirstname() -ne $person.getFirstname()){ $bool = $false } if($this.getLastname() -ne $person.getLastname()){ $bool = $false } return $bool } }
#Class Country, object with Country class Country { #Hidden properties hidden [String] $Name #Country name hidden [Continent] $Continent #Continent object for this country hidden [String] $A2 #Country name with 2 letter hidden [String] $A3 #Country name with 3 letter #Constructor Country(){} #Constructor Country([String] $Name, [Continent] $Continent, [String] $A2, [String] $A3){ $this.Name = $Name $this.Continent = $Continent $this.A2 = $A2 $this.A3 = $A2 } #Write to cli method [void] WriteToCli(){ Write-Host "##### DATA to CLI - Start #####" Write-Host "Name:" $this.Name Write-Host "Continent:" $this.Continent.getName() Write-Host "A2:" $this.A2 Write-Host "A3:" $this.A3 Write-Host "##### DATA to CLI - End #####" } #Get method [String] getName(){ return $this.NAme } #Get method [Continent] getContinent(){ return $this.Continent } #Get method [String] getA2(){ return $this.A2 } #Get method [String] getA3(){ return $this.A3 } #Set method [void] setName([String] $Name){ $this.Name = $Name } #Set method [void] setContinent([Continent] $Continent){ $this.Continent = $Continent } #Set method [void] setA2([String] $A2){ $this.A2 = $A2 } #Set method [void] setA3([String] $A3){ $this.A3 = $A3 } #compare method [boolean] compare([Country] $country){ $bool = $true if( !( $this.getContinent().compare($country.getContinent()) )){ $bool = $false } if($this.getName() -ne $country.getName()){ $bool = $false } if($this.getA2() -ne $country.getA2()){ $bool = $false } if($this.getA3() -ne $country.getA3()){ $bool = $false } return $bool } }
#Class Continet, object with Continent class Continent { #Hidden properties hidden [String] $Name #Continent name #Constructor Country(){} #Constructor Country([String] $Name){ $this.Name = $Name } #Write to cli method [void] WriteToCli(){ Write-Host "##### DATA to CLI - Start #####" Write-Host "Name:" $this.Name Write-Host "##### DATA to CLI - End #####" } #Get method [String] getName(){ return $this.NAme } #Set method [void] setName([String] $Name){ $this.Name = $Name } #compare method [boolean] compare([Continent] $continent){ $bool = $true if($this.getName() -ne $continent.getName()){ $bool = $false } if($this.getName() -ne $continent.getName()){ $bool = $false } return $bool } }
Tydligen blir inte formateringen den bästa när man lägger kod i wordpress, men det får duga.