PowerShell 类:是否可以使构造函数参数可选?

Ral*_*ngs 4 powershell constructor class optional-parameters

我正在尝试为电影/动画中使用的时间码约定构建一个“类型”。

我想适应用户可能希望使用从其他地方获取的时间码字符串初始化对象的用例:

$MyTimeCode = [TimeCode]::new("08:06:04.10", 12)
Run Code Online (Sandbox Code Playgroud)

在其他情况下,直接使用属于类构造函数的参数:

$MyTimeCode = [TimeCode]::new(, 8, 6, 4, 10, 12)

#If I had constructor such as the foloowing in my class definition:
#TimeCode([String]$TimeStamp, [Int]$Hours, [Int]$Minutes, [Int]$Seconds, [Int]$Frames, [Int]$FrameRate)
`
Run Code Online (Sandbox Code Playgroud)

以下面的类为例:

class HelloWorld {
    [String]$Hello
    [String]$World

    HelloWorld([String]$Hello, [String]$World) {
        $This.Hello = $Hello
        $This.World = $World
        }
    }
Run Code Online (Sandbox Code Playgroud)

尝试初始化它[HelloWorld]::new("Hello", "World")工作正常,但[HelloWorld]::new("Hello")出现错误:

MethodException: Cannot find an overload for "new" and the argument count: "1".
Run Code Online (Sandbox Code Playgroud)

我到处看过一些讲座,但它们根本没有结构,而且进步很快。

我通常会去官方文档,但“About_Classes”页面会通过“Rack”和“Devices”示例快速前进到“继承”和“子类”等,我不清楚如何开始仅使用一个班级。

我的目标是熟悉课程及其功能。时间码是次要的。

lit*_*lit 7

需要一个带有单个参数的构造函数。只要每个构造函数的参数签名是唯一的,就可以根据需要有任意多个构造函数。此代码还创建了一个默认构造函数。$null实例化时将包含默认构造函数的成员。

class HelloWorld {
    [String]$Hello
    [String]$World

    HelloWorld([String]$Hello, [String]$World) {
        $This.Hello = $Hello
        $This.World = $World
    }
    HelloWorld([String]$Hello) {
        $This.Hello = $Hello
        $This.World = 'default'
    }
    HelloWorld() {}     # default constructor
}

$x1 = [HelloWorld]::new('hello', 'world')
$x1
$x2 = [HelloWorld]::new('hello')
$x2
$x3 = [HelloWorld]::new()
$x3
Run Code Online (Sandbox Code Playgroud)


mkl*_*nt0 5

lit 的有效解决方案添加一些背景信息

您正在寻找的功能需要以下功能之一,这两个功能在 PowerShellWindows PowerShell(不再积极开发)和积极开发的PowerShell(Core)(从 v7.3 开始)中均不可用。 x,撰写本文时的稳定版本):

  • 可选的方法/构造函数参数,最好与命名参数结合使用。

    • 尽管 PowerShell 允许您定义可选参数,但不支持可选参数:

       # !! Does NOT work as of PowerShell 7.3.x
       # !! The default value is QUIETLY ACCEPTED, but IGNORED. 
       class Foo { [void] Bar($baz, $quux = 42) {} }
      
       # !! FAILS, because you must supply arguments for BOTH parameters.
       # !! -> 'Cannot find an overload for "Bar" and the argument count: "1"'
       [Foo]::new().Bar('hi')
      
      Run Code Online (Sandbox Code Playgroud)
    • 命名参数基本上不受支持。

  • 构造函数链接,即具有多个构造函数重载,可以使用默认值相互调用,但从 v7.3.x 开始,PowerShell 不支持:

       class Foo { 
         # Two-parameter constructor
         Foo($bar, $baz) { <# ... #> } 
         # Single-parameter constructor
         Foo($bar) { 
           # !! Constructor chaining Does NOT work as of PowerShell 7.3.x:
           # !! That is, the attempt to call the two-parameter constructor
           # !! (with a default value for the second parameter) causes a SYNTAX ERROR.
           $this($bar, 'default for $baz') 
         } 
       }
    
    Run Code Online (Sandbox Code Playgroud)
    • 有关通过隐藏辅助方法的解决方法,请参阅此答案

      • 在简单的情况下,如果不关心代码重复,您可以简单地创建单独的、独立的构造函数重载,如 lit 的答案所示。
    • GitHub 问题 #19969是一个功能请求,要求实现构造函数链(以及其他功能)。