Powershell覆盖分配和比较运算符

时间:2019-07-16 19:07:27

标签: powershell class operator-overloading powershell-5.0

使用Powershell 5,您现在可以实现自定义类。最终,您将开始需要让操作员超负荷执行

之类的操作
class Number
{
    [int] $value = 0

    Number() {} # empty constructor

    Number ([int]$somethingElse)
        {$this.value = $somethingElse}
}

$one = [number]::new(1)
$two = [number]::new(2)
$three = $one + $two
if ($three -eq ($one + $two))
    {"3 = 1 + 2"}
else
    {"3 <> 1 + 2 "}

但这似乎没有得到很好的记录,在搜索Stack Overflow之后,我找不到该怎么做

1 个答案:

答案 0 :(得分:1)

通常认为您无法在Powershell中执行此操作。不幸的是,经过几次Google搜索并在Stack Overflow上进行搜索之后,我即将放弃。但是后来我发现this post的要旨,事实证明您可以!

鉴于我很难发现,所以我想为他人提供一个清晰且格式正确的示例(以及当我很快忘记该如何做时我的未来自我)

感谢JFFail将this发布到Github

您将需要适当地重载这些内容,它们应该可以自我解释,但是可以随时询问是否存在某些问题

SetValue([int]$newValue)
GetValue()
[boolean] Equals($otherNumber)
[string] ToString() # you can get more advanced by overloading "IConvertible.ToType" but that's a whole can of worms all it's own
static [MyClass] op_Addition   ([MyClass]$first, [MyClass]$second)
static [MyClass] op_Subtraction([MyClass]$first, [MyClass]$second)
static [MyClass] op_Multiply   ([MyClass]$first, [MyClass]$second)
static [MyClass] op_Division   ([MyClass]$first, [MyClass]$second)

对于-gt-lt,您需要使用以下内容。请注意,尽管它确实支持-eq,但您可以使用Equals(object)本身来获得它,如上所述,并且不需要继承IComparable

class MyClass : System.IComparable 
# requires inheritance from System.IComparable

[int] CompareTo($otherNumber)
# CANNOT declare type in parameter declaration, you'd have to check it later.
# 1 is greater than
# 0 is equal
# -1 is less than
# $null can be returned to indicate invalid comparison

这是一个可行的例子

# If you don't need -lt or -gt then you don't have to inherit from System.IComparable
class Number : System.IComparable
{
    [int] $value = 0

    Number() {} # empty constructor

    Number ([int]$somethingElse)
        {$this.value = $somethingElse}

    [void] SetValue ([int]$newValue)
        {$this.value = $newValue}

    [int] GetValue()
        {return $this.value}

    [boolean] Equals($otherNumber)
    {
        if ($this.value -eq $otherNumber.GetValue())
            {return $true}
        else
            {return $false}
    }

    [int] CompareTo($otherNumber)
    {
        if ($this.value -gt $otherNumber.GetValue()) {return 1}
        if ($this.value -eq $otherNumber.GetValue()) {return 0}
        if ($this.value -lt $otherNumber.GetValue()) {return -1}
        return $null
    }

    [string] ToString()
        {return $this.value.ToString()}

    static [Number] op_Addition ([Number]$first, [Number]$second)
        {return [Number]::new($first.GetValue() + $second.GetValue())}

    static [Number] op_Subtraction([Number]$first, [Number]$second)
        {return [Number]::new($first.GetValue() - $second.GetValue())}

    static [Number] op_Multiply([Number]$first, [Number]$second)
        {return [Number]::new($first.GetValue() * $second.GetValue())}

    static [Number] op_Division([Number]$first, [Number]$second)
        {return [Number]::new($first.GetValue() / $second.GetValue())}
}

# Very Basic Unit Tests
$firstNumber = [Number]::new(8)
$secondNumber = [Number]::new(12)
$thirdNumber = [Number]::new(8)

if ($firstNumber -eq $secondNumber)
    {Write-Output ("{0} == {1}" -F $firstNumber, $secondNumber)}
else
    {Write-Output ("{0} <> {1}" -F $firstNumber, $secondNumber)}

if ($firstNumber -eq $thirdNumber)
    {Write-Output ("{0} == {1}" -F $firstNumber, $thirdNumber)}
else
    {Write-Output ("{0} <> {1}" -F $firstNumber, $thirdNumber)}

switch ($true)
{
    ($firstNumber -gt $secondNumber) {Write-Output ("{0} > {1}" -F $firstNumber, $secondNumber)}
    ($firstNumber -eq $secondNumber) {Write-Output ("{0} == {1}" -F $firstNumber, $secondNumber)}
    ($firstNumber -lt $secondNumber) {Write-Output ("{0} < {1}" -F $firstNumber, $secondNumber)}
}

$fourthNumber = $firstNumber + $secondNumber
Write-Output ("{0} = {1} + {2}" -F $fourthNumber, $firstNumber, $secondNumber)

$fifthNumber = $secondNumber - $thirdNumber
Write-Output ("{0} = {1} - {2}" -F $fifthNumber, $secondNumber, $thirdNumber)

$sixthNumber = $firstNumber * $secondNumber
Write-Output ("{0} = {1} * {2}" -F $sixthNumber, $firstNumber, $secondNumber)

$seventhNumber = $sixthNumber / $firstNumber
Write-Output ("{0} = {1} / {2}" -F $seventhNumber, $sixthNumber, $firstNumber)

这是输出

> 8 <> 12
> 8 == 8
> 8 < 12
> 20 = 8 + 12
> 4 = 12 - 8
> 96 = 8 * 12
> 12 = 96 / 8