我在Powershell 5中创建了一个简单的自定义类,它将字母数字字符串转换为整数。
用法:
$test = [newclass]::new("B8")
$test.Value // 118 # type int
$test.ToString() // B8 # type string
有没有办法通过仅引用Value
变量来返回118(当前使用$test
属性?)
基本上我希望$test
无需使用118
属性即可返回Value
。
Class newclass {
# Hidden properties
hidden [string]$AlphaNumericValue
# Properties
[int]$Value
# Methods
[string]ToString() {
Return $this.AlphaNumericValue
}
# Constructors
newclass([string]$String) {
$this.AlphaNumericValue = $String
$this.Value = If ($String -match "^0[0-9]$") {
$String.Substring(1)
}
ElseIf ($String -match "^[1-9][0-9]$") {
$String
}
ElseIf ($String -match "^[a-zA-Z][0-9]$") {
Switch ($String.Substring(0,1)) {
"A" {"10$($String.Substring(1))"}
"B" {"11$($String.Substring(1))"}
"C" {"12$($String.Substring(1))"}
"D" {"13$($String.Substring(1))"}
"E" {"14$($String.Substring(1))"}
"F" {"15$($String.Substring(1))"}
"G" {"16$($String.Substring(1))"}
"H" {"17$($String.Substring(1))"}
"I" {"18$($String.Substring(1))"}
"J" {"19$($String.Substring(1))"}
"K" {"20$($String.Substring(1))"}
"L" {"21$($String.Substring(1))"}
"M" {"22$($String.Substring(1))"}
"N" {"23$($String.Substring(1))"}
"O" {"24$($String.Substring(1))"}
"P" {"25$($String.Substring(1))"}
"Q" {"26$($String.Substring(1))"}
"R" {"27$($String.Substring(1))"}
"S" {"28$($String.Substring(1))"}
"T" {"29$($String.Substring(1))"}
"U" {"30$($String.Substring(1))"}
"V" {"31$($String.Substring(1))"}
"W" {"32$($String.Substring(1))"}
"X" {"33$($String.Substring(1))"}
"Y" {"34$($String.Substring(1))"}
"Z" {"35$($String.Substring(1))"}
}
}
Else {
throw "Invalid format"
}
}
}
答案 0 :(得分:0)
您可以添加一个可以执行此操作的方法:
static [int] justValue ([string]$input)
{
return ([newclass]::new($input)).Value
}
在最后一次之前插入}
C:\> [newclass]::justValue("B8")
118
C:\>