[英]Classes in Powershell 5, need help creating $foo.bar.run()
I seriously apologize for the horrible title of this post...but I'm new to OOP and PowerShell, so I have absolutely no idea how to spell out what I'm asking for. 我为这篇文章的可怕标题道歉...但我是OOP和PowerShell的新手,所以我完全不知道如何拼出我要求的内容。 After finding out I could use PowerShell to connect to API's and things like that, I've been working on it non-stop.
在发现我可以使用PowerShell连接到API之类的东西之后,我一直在不间断地工作。 I'm now working on a script where I need to create a class.
我现在正在编写一个我需要创建一个类的脚本。 I've done this successfully and I'm able to build methods and use them.
我已经成功完成了这项工作,我可以构建方法并使用它们。 As of now, all of my objects and methods only have a single level....So if I call a method on an object, it always looks like this $foo.run()
截至目前,我的所有对象和方法只有一个级别....所以如果我在一个对象上调用一个方法,它总是看起来像$ foo.run()
But I want to create something like $foo.bar.run() 但我想创建类似$ foo.bar.run()的东西
I've written a script that uses my TV's API. 我编写了一个使用我的电视API的脚本。 So currently I have commands like this:
所以目前我有这样的命令:
$tv.TurnOn()
$tv.TurnOff()
$tv.GetPowerStatus()
$tv.ListInputs()
$tv.GetCurrentInput()
$tv.SetInput($name)
But I'd like to be able to do this instead: 但我希望能够做到这一点:
$tv.Power.On()
$tv.Power.Off()
$tv.Power.Status()
$tv.Input.List()
$tv.Input.Current()
$tv.Input.Set($name)
Is this possible? 这可能吗?
And yes, I realize there are probably other languages that might be better suited. 是的,我意识到可能还有其他语言可能更适合。 But I'm just playing around and using this as a transitioning language into actual C# development.
但我只是在玩这个并将其用作实际C#开发的过渡语言。 For me it's easier to throw a bunch of scripts and parameters into a file and run the file than trying to set up a C# project, compile and test.
对我来说,将一堆脚本和参数放入文件并运行文件比尝试设置C#项目,编译和测试更容易。
Here is the current code for the class: 这是该类的当前代码:
Class VizioTV {
[String]$IPAddress
[String]$AuthToken
[void]TurnOn() {
Set-Power -action "on" -IPAddress $this.IPAddress -auth $this.AuthToken
}
[void]TurnOff() {
Set-Power -action "off" -IPAddress $this.IPAddress -auth $this.AuthToken
}
[String]GetPowerStatus() {
Return Get-PowerStatus -IPAddress $this.IPAddress -auth $this.AuthToken
}
}
Define a new class for the Power
functions and inject the IPAddress and Auth token into a new instance of that class in the constructor of the main class: 为
Power
函数定义一个新类,并将IPAddress和Auth标记注入主类构造函数中该类的新实例:
class VizioTVPower {
hidden [String]$IPAddress
hidden [String]$AuthToken
VizioTVPower([string]$IPAddress, [string]$AuthToken) {
$this.IPAddress = $IPAddress
$this.AuthToken = $AuthToken
}
[void]TurnOn() {
Set-Power -action "on" -IPAddress $this.IPAddress -auth $this.AuthToken
}
[void]TurnOff() {
Set-Power -action "off" -IPAddress $this.IPAddress -auth $this.AuthToken
}
[String]GetPowerStatus() {
Return Get-PowerStatus -IPAddress $this.IPAddress -auth $this.AuthToken
}
}
class VizioTV {
[String]$IPAddress
[String]$AuthToken
[VizioTVPower]$Power
VizioTV([string]$IPAddress, [string]$AuthToken) {
$this.IPAddress = $IPAddress
$this.AuthToken = $AuthToken
$this.Power = [VizioTVPower]::new($this.IPAddress, $this.AuthToken)
}
}
You could create classes for Power
and Input
and store them as properties in your TV-class. 您可以为
Power
和Input
创建类,并将它们存储为电视类中的属性。 Remember to pass a reference to the parent object (TV) so they can access the IPAddress
and AuthToken
-values. 请记住传递对父对象(TV)的引用,以便它们可以访问
IPAddress
和AuthToken
。
class VizioTVInput {
[VizioTV]$TV
VizioTVInput([VizioTV]$TV) {
#Keep reference to parent
$this.TV = $TV
}
[string[]]List() {
return "Something"
}
[string]Current() {
return "Something"
}
[void]Set([string]$name) {
#Do something with $name
}
}
class VizioTVPower {
[VizioTV]$TV
VizioTVPower([VizioTV]$TV) {
#Keep reference to parent
$this.TV = $TV
}
[void]On() {
#Remove Write-Host, just used for demo
Write-Host Set-Power -action "on" -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
}
[void]Off() {
Set-Power -action "off" -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
}
[String]Status() {
return Get-PowerStatus -IPAddress $this.TV.IPAddress -auth $this.TV.AuthToken
}
}
Class VizioTV {
[String]$IPAddress
[String]$AuthToken
[VizioTVInput]$Input = [VizioTVInput]::new($this)
[VizioTVPower]$Power = [VizioTVPower]::new($this)
#Made it mandatory to input IP and AuthToken. Remove constructor if not needed
VizioTV([string]$IPAddress,[string]$AuthToken) {
$this.IPAddress = $IPAddress
$this.AuthToken = $AuthToken
}
}
$TV = New-Object VizioTV -ArgumentList "127.0.0.1", "AuthKey123"
Demo: 演示:
$TV.Power.On()
#Outputs
Set-Power -action on -IPAddress 127.0.0.1 -auth AuthKey123
$TV.IPAddress = "10.0.0.1"
$TV.Power.On()
#Outputs
Set-Power -action on -IPAddress 10.0.0.1 -auth AuthKey123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.