我正在學習物件和類,并跟隨另一個站點上的代碼。它創建了一個新的類Student,它接受firstName,lastName,score的生日,和多個變數。
class Student {
[string]$firstName = ""
[string]$lastName = ""
[int]$score = 3
[System.DateTime]$birthday
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month, [int]$year) {
$this.firstName = $firstName
$this.lastName = $lastName
$this.score = $score
$this.birthday = (Get-Date -minute 0 -second 0 -millisecond 0 -hour 0 -year $year -Day)
}
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month) {
$this.firstName = $firstName
$this.lastName = $firstName
$this.score = $score
$this.birthday = (Get-Date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day $day -Month $month)
}
[string] GetFullName() {
return ("{0} {1}" -f $this.firstName, $this.lastName)
}
[string] GetReportLine() {
return ("{0} {1}" -f $this.firstName, $this.lastName)
}
}
正如我所說,我正在學習,但我認為GetReportLine()會顯示名字和姓氏。但是,當我創建一個新的 Student 物件時
[Student]::new
$some = [Student]::new("Some", "Guy", 3, 2, 2)
然后嘗試呼叫它,完全按照在線所示撰寫所有代碼,我將其作為輸出
$some.GetReportLine()
一些 一些
當我期待這是輸出時
一些人
所以,我得出的結論是代碼中存在錯誤,或者我只是不明白代碼是如何正確作業的。
大家能不能檢查一下,讓我知道是哪個?我只是想繞過它。
uj5u.com熱心網友回復:
如果只有 IDE 能夠捕捉到這種錯誤,我經常會遇到這種錯誤。您的代碼按書面方式作業...lastname = $firstName。
class Student
{
Student ([string]$firstName, [string]$lastName, [int]$score, [int]$day, [int]$month) {
$this.firstName = $firstName
# ↓↓↓↓ ????? !This is your problem! ?????
$this.lastName = $firstName
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338093.html
