forked from naeemkhedarun/psclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimalTest.ps1
108 lines (83 loc) · 1.95 KB
/
AnimalTest.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
$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 {
$private.Name
} -set {
param($newName)
Write-Host "Renaming $($this.Class.ClassName) '$($private.Name)' to '$($newName)'"
$private.Name = $newName
}
property Legs {
$private.Legs
}
method -override ToString {
"A $($this.Class.ClassName) named $($this.name) with $($this.Legs) Legs"
}
method Speak {
Throw "not implemented"
}
}
$DogClass = New-PSClass -inherit $AnimalClass Dog {
note -static ObjectCount 0
method -static DisplayObjectCount {
"$($this.ClassName) has $($this.ObjectCount) instances"
}
constructor {
param($DogName)
Base $DogName 4
$DogClass.ObjectCount += 1
}
method -override ToString {
"$(Invoke-BaseClassMethod 'ToString') with fluf"
}
method -override Speak {
"Arf"
}
method EatFood {
#[CmdletBinding()]
param (
#[parameter( Mandatory=$true, HelpMessage=”Food Kind is required”)]
[String]
[ValidateNotNullOrEmpty()]
$FoodKind
)
"Doogy {0} is eating some {1}! - Yummy!" -f $this.Name, $FoodKind
}
}
$BirdClass = New-PSClass -inherit $AnimalClass Bird {
note -static ObjectCount 0
method -static DisplayObjectCount {
"$($this.ClassName) has $($this.ObjectCount) instances"
}
constructor {
Base $Args[0] 2
$BirdClass.ObjectCount += 1
}
method -override Speak {
"Squawk"
}
}
$Dog = $DogClass.New("Bowser")
$Dog.ToString()
$Dog.Name = "Duke"
$Dog.ToString()
$Dog.Speak()
$Bird = $BirdClass.New("Tweedy")
$Bird.ToString()
$Bird.Speak()
$DogClass.DisplayObjectCount()
$BirdClass.DisplayObjectCount()
$AnimalClass.DisplayObjectCount()