「Windows/powershell/クラス」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
(→static変数) |
||
| (同じ利用者による、間の15版が非表示) | |||
| 行1: | 行1: | ||
| − | ==サンプル== | + | ==サンプル(インスタンス生成)== |
HelloClass.ps1 | HelloClass.ps1 | ||
<pre> | <pre> | ||
| 行5: | 行5: | ||
[string] hello() { | [string] hello() { | ||
return "Hello!!" | return "Hello!!" | ||
| + | } | ||
| + | [void] test() { | ||
| + | Write-Host "hoge" | ||
} | } | ||
} | } | ||
$helloObj = New-Object HelloClass | $helloObj = New-Object HelloClass | ||
| + | # $helloObj = [HelloClass]::new() #こちらでもよい | ||
| + | $helloObj.hello() | ||
| + | $helloObj.test() # hoge | ||
| + | </pre> | ||
| + | 参考:https://www.vwnet.jp/Windows/PowerShell/2017082401/PSv5Class06.htm | ||
| + | |||
| + | ==サンプル(コンストラクト)== | ||
| + | HelloClass.ps1 | ||
| + | <pre> | ||
| + | class HelloClass { | ||
| + | [string] $name | ||
| + | HelloClass([string] $name) { | ||
| + | Write-Host $name | ||
| + | $this.name = $name | ||
| + | } | ||
| + | [string] hello() { | ||
| + | return "Hello!! " + $this.name | ||
| + | } | ||
| + | } | ||
| + | $helloObj = [HelloClass]::new("taro") | ||
$helloObj.hello() | $helloObj.hello() | ||
</pre> | </pre> | ||
| 行21: | 行44: | ||
} | } | ||
[HelloUtil]::hello(11) | [HelloUtil]::hello(11) | ||
| + | </pre> | ||
| + | |||
| + | ==ハッシュの値を返す== | ||
| + | ハッシュの型はhashtableになる。 | ||
| + | <pre> | ||
| + | class HashClass { | ||
| + | static [hashtable] load() { | ||
| + | $hash = @{ Number = 1; Shape = "Square"; Color = "Blue"} | ||
| + | return $hash | ||
| + | } | ||
| + | } | ||
| + | $config1 = [IniClass]::load() | ||
| + | Write-Host $config1.number # 1 | ||
| + | </pre> | ||
| + | |||
| + | ===globalにしたデータは取得できない=== | ||
| + | <pre> | ||
| + | class HashClass { | ||
| + | static [hashtable] load() { | ||
| + | $global.hash = @{ Number = 1; Shape = "Square"; Color = "Blue"} | ||
| + | return $hash | ||
| + | } | ||
| + | } | ||
| + | $hashData = [HashClass]::load() | ||
| + | > Unable to find type [HashClass]. | ||
| + | </pre> | ||
| + | ==staticメソッドからstaticメソッドを呼ぶ== | ||
| + | もしくは、クラスメソッドから、クラスメソッドを呼ぶ。 | ||
| + | |||
| + | 初回staticメソッドがを呼ぶ前のクラス読み込み前に、その先のメソッドを呼び出しておく。 | ||
| + | |||
| + | 例:mainからExample.hello()からLog.info()を呼び出す場合は、 | ||
| + | main.csに以下順序で、呼び出す。 | ||
| + | <pre> | ||
| + | . ./Log.cs | ||
| + | . ./Example.cs | ||
| + | ・・・処理 | ||
| + | </pre> | ||
| + | |||
| + | ==static変数== | ||
| + | <pre> | ||
| + | class Log { | ||
| + | static [int] $level | ||
| + | [void] SetLevel([int] $level) { | ||
| + | [Log]::level = $level | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==クラスを別ファイルにする場合== | ||
| + | HelloClass.ps1 | ||
| + | <pre> | ||
| + | class HelloClass { | ||
| + | [string] hello() { | ||
| + | return "Hello!!" | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | main.ps1 | ||
| + | <pre> | ||
| + | . ./HelloClass.ps1 | ||
| + | $helloObj = New-Object HelloClass | ||
| + | $helloObj.hello() | ||
| + | </pre> | ||
| + | |||
| + | ==クラス内のメソッドから、別メソッドを呼び出す== | ||
| + | $this.を使えば良い。 | ||
| + | <pre> | ||
| + | class HelloClass { | ||
| + | [string] main() { | ||
| + | $this.hello() | ||
| + | } | ||
| + | [string] hello() { | ||
| + | return "Hello!!" | ||
| + | } | ||
| + | } | ||
</pre> | </pre> | ||
2021年9月2日 (木) 18:35時点における最新版
目次
サンプル(インスタンス生成)
HelloClass.ps1
class HelloClass {
[string] hello() {
return "Hello!!"
}
[void] test() {
Write-Host "hoge"
}
}
$helloObj = New-Object HelloClass
# $helloObj = [HelloClass]::new() #こちらでもよい
$helloObj.hello()
$helloObj.test() # hoge
参考:https://www.vwnet.jp/Windows/PowerShell/2017082401/PSv5Class06.htm
サンプル(コンストラクト)
HelloClass.ps1
class HelloClass {
[string] $name
HelloClass([string] $name) {
Write-Host $name
$this.name = $name
}
[string] hello() {
return "Hello!! " + $this.name
}
}
$helloObj = [HelloClass]::new("taro")
$helloObj.hello()
参考:https://www.vwnet.jp/Windows/PowerShell/2017082401/PSv5Class06.htm
サンプル静的クラス
HelloUtil.ps1
class HelloUtil {
static [int] hello([int] $test) {
return $test
}
}
[HelloUtil]::hello(11)
ハッシュの値を返す
ハッシュの型はhashtableになる。
class HashClass {
static [hashtable] load() {
$hash = @{ Number = 1; Shape = "Square"; Color = "Blue"}
return $hash
}
}
$config1 = [IniClass]::load()
Write-Host $config1.number # 1
globalにしたデータは取得できない
class HashClass {
static [hashtable] load() {
$global.hash = @{ Number = 1; Shape = "Square"; Color = "Blue"}
return $hash
}
}
$hashData = [HashClass]::load()
> Unable to find type [HashClass].
staticメソッドからstaticメソッドを呼ぶ
もしくは、クラスメソッドから、クラスメソッドを呼ぶ。
初回staticメソッドがを呼ぶ前のクラス読み込み前に、その先のメソッドを呼び出しておく。
例:mainからExample.hello()からLog.info()を呼び出す場合は、 main.csに以下順序で、呼び出す。
. ./Log.cs . ./Example.cs ・・・処理
static変数
class Log {
static [int] $level
[void] SetLevel([int] $level) {
[Log]::level = $level
}
}
クラスを別ファイルにする場合
HelloClass.ps1
class HelloClass {
[string] hello() {
return "Hello!!"
}
}
main.ps1
. ./HelloClass.ps1 $helloObj = New-Object HelloClass $helloObj.hello()
クラス内のメソッドから、別メソッドを呼び出す
$this.を使えば良い。
class HelloClass {
[string] main() {
$this.hello()
}
[string] hello() {
return "Hello!!"
}
}
