Python代码转Windows平台PowerShell代码及报错问题求助
转换存在的问题
- 属性类型错误:原Python代码中
extensions是存储多个后缀的列表,你转换后错误定义为单个[string] $extension单字符串类型,无法存储多个值,也不支持追加操作。 - 冗余属性定义:子类
Dossier继承自Item后,无需重复定义父类已有的$name、$path、$modification属性。 - 构造函数赋值缺失:
Dossier构造函数最后一行仅写了$pathBakcup,没有加$this.前缀,无法完成该属性的赋值操作。 - 语法逻辑错误:
- 元素存在性判断写法错误,PowerShell中判断元素不在集合内应使用
-notin操作符,你写的-NOT $this.$extension完全不符合语法规则 - 调用自身属性时多加了
$符号,错误写法$this.$extension应为$this.extensions - 集合追加方法错误,PowerShell中泛型列表的追加方法是
Add(),不是Python的append()
- 元素存在性判断写法错误,PowerShell中判断元素不在集合内应使用
- 括号闭合缺失:
AddExtension方法和Dossier类都缺少对应的闭合大括号}
修正后的完整PowerShell代码
class Item { [String] $name [String] $path [String] $modification Item([String] $name, [String] $path,[String] $modification) { $this.name = $name $this.path = $path $this.modification= $modification } } class Dossier:Item { [String] $creation [System.Collections.Generic.List[String]] $extensions [String] $pathBakcup Dossier([String] $name, [String] $path,[String] $modification,[String]$creation,[String]$extension,[String]$pathBakcup):base($name, $path, $modification) { $this.creation=$creation $this.extensions = [System.Collections.Generic.List[String]]::new() if($extension){ $this.extensions.Add($extension) } $this.pathBakcup = $pathBakcup } [void] AddExtension([string]$extension) { if ($extension -notin $this.extensions) { $this.extensions.Add($extension) } } }
内容的提问来源于stack exchange,提问作者dingdigndignodng
相关产品推荐
相关产品推荐

