簡體   English   中英

為什么我不能在Powershell 5的函數中訪問類變量?

[英]Why can't I access class variables in function in Powershell 5?

我試圖在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.

使用$this訪問變量:

class Foo{
      $bar = 'foobar'
      [string] mymethod(){
        return $this.bar + '123'
      }
    }

輸出:

foobar123

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM