「Windows/powershell/クラス」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
(→サンプル) |
||
| 行1: | 行1: | ||
| − | ==サンプル== | + | ==サンプル(インスタンス生成)== |
HelloClass.ps1 | HelloClass.ps1 | ||
<pre> | <pre> | ||
| 行14: | 行14: | ||
$helloObj.hello() | $helloObj.hello() | ||
$helloObj.test() # hoge | $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() | ||
</pre> | </pre> | ||
参考:https://www.vwnet.jp/Windows/PowerShell/2017082401/PSv5Class06.htm | 参考:https://www.vwnet.jp/Windows/PowerShell/2017082401/PSv5Class06.htm | ||
2021年8月23日 (月) 16:51時点における版
サンプル(インスタンス生成)
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)
