18

我正在做一些测试并偶然发现以下内容:

您可以根据需要重载 PoShv5 中的方法。如果你调用不带参数的方法,它可以在内部调用带参数的方法,以保持你的代码非冗余。我希望构造函数也是如此。

在此示例中,最后一个构造函数按预期工作。其他构造函数只返回没有设置值的对象。

Class car {
    [string]$make
    [string]$model
    [int]$Speed
    [int]$Year

    speedUp (){
        $this.speedUp(5)
    }
    speedUp ([int]$velocity){
        $this.speed += $velocity
    }

    # Constructor
    car () {
        [car]::new('mall', $Null, $null)
    }

    car ([string]$make, [string]$model) {
        [car]::new($make, $model, 2017)
    }

    car ([string]$make, [string]$model, [int]$Year) { 
        $this.make = $make
        $this.model = $model
        $this.Year = $year
    }
}

[car]::new() # returns "empty" car
[car]::new('Make', 'Nice model') # returns also an "empty" one
[car]::new( 'make', 'nice model', 2017) # returns a "filled" instance

有没有办法解决这个问题?我错过了什么?

4

2 回答 2

30

为了补充Mathias R. Jessen 的有用答案

推荐的方法是使用隐藏的辅助方法来弥补构造函数链接的不足

Class car {

    [string]$Make
    [string]$Model
    [int]$Year

    speedUp (){
        $this.speedUp(5)
    }
    speedUp ([int]$velocity){
        $this.speed += $velocity
    }

    # Hidden, chained helper methods that the constructors must call.
    hidden Init([string]$make)                 { $this.Init($make, $null) }
    hidden Init([string]$make, [string]$model) { $this.Init($make, $model, 2017) }
    hidden Init([string]$make, [string]$model, [int] $year) {
        $this.make = $make
        $this.model = $model
        $this.Year = $year
    }

    # Constructors
    car () {
        $this.Init('Generic')
    }

    car ([string]$make) {
        $this.Init($make)
    }

    car ([string]$make, [string]$model) {
        $this.Init($make, $model)
    }

    car ([string]$make, [string]$model, [int]$year) { 
        $this.Init($make, $model, $year)
    }
}

[car]::new()                          # use defaults for all fields
[car]::new('Fiat')                    # use defaults for model and year
[car]::new( 'Nissan', 'Altima', 2015) # specify values for all fields

这产生:

Make    Model  Year
----    -----  ----
Generic        2017
Fiat           2017
Nissan  Altima 2015

笔记:

  • hidden关键字更多的是 PowerShell 本身遵守的约定例如在输出时省略此类成员);但是,以这种方式标记的成员在技术上仍然可以访问。

  • 虽然您不能直接调用同一类的构造函数,但可以使用基类构造函数,使用类似 C# 的语法来调用。

于 2017-06-07T13:47:20.710 回答
13

TL;DR:不!


您正在寻找的内容(重载的构造函数相互调用)也通俗地称为构造函数链接,在 C# 中大致如下所示:

class Car
{
    string Make;
    string Model;
    int Year;

    Car() : this("mall", null)
    {
    }

    Car(string make, string model) : this(make, model, 2017) 
    {
    }

    Car(string make, string model, int Year) 
    { 
        this.Make = make;
        this.Model = model;
        this.Year = year;
    }
}

不幸的是,PowerShell 似乎没有任何语法- 你不能这样做:

Car() : $this("Porsche") {}
Car([string]$Make) {}

没有让解析器因为错过构造函数的主体定义而向您抛出,而且我不希望很快看到它 - PowerShell 团队已经表达了一个明确的愿望,不成为新的淡化的维护者C#- 我完全可以理解:-)

您只需在每个构造函数定义中重新实现成员分配。

于 2017-06-07T13:21:18.310 回答