Project Description
Object Oriented Development in PowerShell. PowerShell is great at letting you 'use' all kinds of objects. However, you couldn't write Object Oriented Scripts until now. PSClass implements Inheritance, Polymorphism, encapsulation, and more! Better yet, it's all written in PS!

PsClass is currently implemented in Powershell in a single file. Makes it simple to use. It supports the following OO concepts.
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Constructors with parameters
  • Notes – read-write variables
  • Methods – scriptblocks
  • Properties with Get scriptblocks and optional Set scriptblocks
  • Static and Private Notes and Methods

Below is a simple example demonstrating many of these features. See the Documentation for more help.
Let's walk through an example that demonstrates many of these.

First we'll define a base class called Animal

$AnimalClass = New-PSClass Animal {

  note -static ObjectCount 0
  method -static DisplayObjectCount {
    "$($this.ClassName) has $($this.ObjectCount) instances"
  }

  note -private Name
  note -private Legs

  constructor {
    param ($name,$legs)
    $private.Name = $name
    $private.Legs = $legs
    
    $AnimalClass.ObjectCount += 1
  }
  
  property Name -get {
    $private.Name
  } -set {
    param($newName)
    Write-Host "Renaming $($this.Class.ClassName) '$($private.Name)' to '$($newName)'"
    $private.Name = $newName
  }
  
  property Legs -get {
    $private.Legs
  }
  
  method -override ToString {
    "A $($this.Class.ClassName) named $($this.name) with $($this.Legs) Legs"
  }
} 

This class implements:

Static note variable ObjectCount
It contains the number instances that that have been created. It is referenced as:
  $AnimalClass.ObjectCount

Static method DisplayObjectCount
It contains the number instances that that have been created. It is referenced as:
  $AnimalClass.DisplayObjectCount()

private variables Name and Legs
These are only assesible within the class using the $private variable. Examples
    $private.Name = "Bill"
    $private.Legs = 6

Constructor with Name and Legs Parameters
This gets bound to the New Method for the Class
    $Bill = $AnimalClass.New("Bill",6)

Read/Write Property Name
An Name property can be accessed or changed.
    $Bill = $AnimalClass.New("Bill",6)
    Write-Host $Bill.Name
    $Bill.Name = "Billy"
    Write-Host $Bill.Name

Read-only Property Legs
An Name property can be accessed or changed.
    $Bill = $AnimalClass.New("Bill",6)
    Write-Host $Bill.Legs

Override Method ToString()
Overrides the base ToString method.
    $Bill = $AnimalClass.New("Bill",6)
    Write-Host $Bill.ToString()


Create Inherited Dog Class
This class demonstrates a new PSClass Dog inherited from our example Animal class
$DogClass = New-PSClass -inherit $AnimalClass Dog {
  constructor {
    param($DogName)
    Base $DogName 4   
  }
 
  method -override ToString  {
    "$(Invoke-BaseClassMethod 'ToString') with fluf"
  }

  method EatFood { 
    param ( 
        [String]
        [ValidateNotNullOrEmpty()]
        $FoodKind 
     )
    "Doogy {0} is eating some {1}! - Yummy!" -f $this.Name, $FoodKind
  } 
}

This example also demonstrates V2 Function Validation

Constructor with Base Constructor call
Constructor Class with Name parameter. It calls the base constructor with the name and specifies 4 legs
    $Bowser = $DogClass.New("Bowser")

Override Method ToString()
Overrides the base ToString method providing polymorphic behavior.
    $Bowser = $DogClass.New("Bowser")
    Write-Host $Bowser.ToString()

Adds Method EatFood
New Method allowing the Dog to Eat.
    $Bowser = $DogClass.New("Bowser")
    Write-Host $Bowser.EatFood("Steak")

Last edited Nov 4, 2009 at 2:45 PM by cashfoley, version 9