thi*_*123 1 powershell powershell-5.0
我试图在Powershell v5中定义一个类,我无法从类函数中访问变量.
防爆.
PS C:\> class Foo{
$bar = 'foobar'
mymethod(){
$bar + '123'
}
}
PS C:\> [Foo]::new().mymethod()
PS C:\> At line:4 char:11
+ $bar + '123'
Variable is not assigned in the method.
Run Code Online (Sandbox Code Playgroud)
使用$this访问您的变量:
class Foo{
$bar = 'foobar'
[string] mymethod(){
return $this.bar + '123'
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
foobar123
Run Code Online (Sandbox Code Playgroud)