1.17.25. fejezet, Osztályok

Egy egyszerű példa osztályokra

class Utils {
 
    Utils () {}
 
    static [int] $ISumTotal = 0
    [double] $DSumTotal = 0.0
 
    Utils ($base) {$this.DSumTotal = $base}
 
    static [int] Sum([int[]] $na){
        $result = 0
        if ($na -eq $null -or $na.Length -eq 0) {
            return $result    
        }
        foreach ($n in $na) {
            $result += $n
        }
        [utils]::ISumTotal += $result
        return $result  
    }
 
    [double] DSum([double[]] $da){
        [double] $result = 0.0
        if ($da -eq $null -or $da.Length -eq 0) {
              return $result
        }
        foreach ($n in $da) {
              $result += $n
        }
        $this.DSumTotal += $result
        $this.log($this.DSumTotal)
        return $result
     }
     hidden [void] log($msg) {
        $msg
 
   }
 }
[utils]::Sum(1..10)
$ui = [utils]::new(10)
$values = 1.2,10.5
$ui.DSum($values)
$ui | Get-Member -MemberType Method
"`n"
$ui | Get-Member -Static -MemberType Property
#55
#111,69
#111,69
#
#
#   TypeName: utils
#
#Name        MemberType Definition                     
#----        ---------- ----------                     
#DSum        Method     double DSum(double[] da)       
#Equals      Method     bool Equals(System.Object obj) 
#GetHashCode Method     int GetHashCode()              
#GetType     Method     type GetType()                 
#ToString    Method     string ToString()              
#
#ISumTotal   Property   static int ISumTotal {get;set;}

Metódus overloading

    [double] Sum([double[]] $da){
...
    }
    [int] Sum([int[]] $da){
...
    }

Osztály leszármaztatás, metódus override

class newutils : utils { 
 [int] Sum([int[]] $na){
     return ([utils]$this).Sum([int[]] $na)
 }
}