Search notes:

PowerShell: class inheritance

A class is derived from another class with the following syntax: class derived : baseClass { … }
A constructor can call the base class' constructor with this syntax: derived(…) : base(…) { }

Simple example

class B {
   [int] $num

   B([int] $n) {
      $this.num = $n
   }

   [void] method() {
      write-host "$($this.num)"
   }
}

class D : B {

  [string] $txt

   D([int] $num, [string] $txt)  :  base($num)
   {
      $this.txt = $txt
   }

   [void] method()
   {
      write-host "$($this.num): $($this.txt)"
   }

}

$b = [B]::new(99)
$b.method()

$d = [D]::new(42, 'hello world')
$d.method()
Github repository about-PowerShell, path: /language/class/inheritance/simple.ps1

Index