2
我最近开始使用PowerShell 5.创建类当我在下面这真棒指南https://xainey.github.io/2016/powershell-classes-and-concepts/#methods是否有可能重写PowerShell 5类中的Getter/Setter函数?
我想知道是否有可能重写get_x
和set_x
方法。
例子:
Class Foobar2 {
[string]$Prop1
}
$foo = [Foobar2]::new()
$foo | gm
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Prop1 Property string Prop1 {get;set;}
我想这样做,因为我认为这将是更容易为对方不是用我的自定义Get
和Set
方法访问属性:
Class Foobar {
hidden [string]$Prop1
[string] GetProp1() {
return $this.Prop1
}
[void] SetProp1([String]$Prop1) {
$this.Prop1 = $Prop1
}
}
太好了。这甚至似乎工作,如果我在类构造函数中添加新的变量。不幸的是,我不能覆盖现有的属性,如[Gist示例](https://gist.github.com/OCram85/03ce8c0f881477c835e3fdfc279dfed7) – OCram85
@ OCram85不,您必须使用隐藏的后台字段,例 –