我有一個類,看起來如下:
我有一個類。
export class Lifter {
id: string;
name: string;
weightclass: 數字。
bestSnatch: 數字。
bestCJ: number;
bestTotal: 數字。
bio: string;
imageURL: string;
constructor(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, bio="默認傳記文本", imageURL = "https://upload. wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg"/span>) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
this.bio = bio;
this.imageURL = imageURL;
}
}
所以在建構式中,我為屬性 "bio "和 "imageURL "傳遞默認值。當我通過以下方式創建一個新的Lifter時,這一切都很正常:
new Lifter("1"/span>, "Jesper",73,107,125,220)。)
Bio和ImageURL的默認值已被成功添加。現在是我的問題。
是否有可能為imageURL-perty輸入一個值而不向bio-perty傳遞一個值?在某些情況下,我只想添加一個ImageURL,有時只想添加一個bio,有時兩者都是,有時兩者都不是。我希望這個問題和這個問題已經有點清楚了。
我希望運作的不同呼叫如下:
我希望運作的不同呼叫如下:
new Lifter("1"/span>,"Jesper"/span>, 73,107,125,220), /None of the propertiesnew Lifter("2"/span>,"Alfred"/span>,83, 187,205,220,"Some Biography"), /Biography passed.
new Lifter("3"/span>,"Manfred"/span>, 73,107,125,220, "https: //www. w3schools.com/howto/img_avatar2.png"),//ImageURL passed。
new Lifter("4"/span>,"Sigfred"/span>,73, 107,125,220,"其他一些傳記" ,"https: //www. w3schools.com/howto/img_avatar2.png"),//兩個屬性的值都通過。
為了說明問題,我只是對上面的第三種情況有意見。
uj5u.com熱心網友回復:
一種方法是通過命名引數的物件重構作業法來實作。
你的建構式對作為引數傳入的物件進行解構:
你的建構式對作為引數傳入的物件進行解構。
constructor(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, {bio="default bio", imageURL = "https://upload. wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg"/span>} = {}) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
this.bio = bio;
this.imageURL = imageURL;
}
而且你可以通過傳入適當的物件來這樣呼叫它:
new Lifter("1"/span>, "Jesper",73,107, 125, 220)
new Lifter("2"/span>,"Alfred"/span>, 83,187,205,220, {bio: "一些傳記"})
new Lifter("3"/span>,"Manfred"/span>, 73,107,125,220, {imageURL: "https://www.w3schools.com/howto/img_avatar2.png"})
new Lifter("4"/span>,"Sigfred"/span>, 73,107,125,220, {bio: "其他一些傳記" , imageURL: "https://www.w3schools.com/howto/img_avatar2.png"})
uj5u.com熱心網友回復:
你可以為bio傳入一個null,或者你可以像上面所說的那樣讓它成為可選項。<當你創建一個新的類的實體時,bio必須有一個值,因為這是你定義該類的方式。
new Lifter("3"/span>,"Manfred"/span>, 73,107,125,220,null, "https: //www. w3schools.com/howto/img_avatar2.png"),//ImageURL passed。
uj5u.com熱心網友回復:
除了已經描述過的方法外,還有兩種方法我只想提一下:
export class Lifter {
id: string;
name: string;
weightclass: 數字。
bestSnatch: 數字。
bestCJ: number;
bestTotal: 數字。
bio: string = "默認傳記文本"。
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
private constructor(id: string, name: string, weightclass: number, bestSnatch: number, bestCJ: number, bestTotal: number) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
static create(id: string, name: string, weightclass: 數字, bestSnatch:數字, bestCJ:數字, bestTotal:數字, Bio: string, imageURL: string): Lifter {
return new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal)。
}
static createWithDefaultBioAndImageURL(id: string,name: string, weightclass:數字, bestSnatch:數字, bestCJ:數字, bestTotal:數字)。) Lifter {
return new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal)。
}
static createWithDefaultBio(id: string, name: string, weightclass: 數字, bestSnatch:數字, bestCJ:數字, bestTotal:數字, imageURL: string)。) Lifter {
const lifter = new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal)。
lifter.imageURL = imageURL。
return lifter。
}
static createWithDefaultImageURL(id: string, name: string, weightclass: 數字, bestSnatch:數字, bestCJ:數字, bestTotal:數字, bio: string)。) Lifter {
const lifter = new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal)。
lifter.bio = bio;
return lifter;
}
}
const lifter1 = Lifter. create("3"/span>,"Manfred"/span>,73。 107,125,220," sampleBio", "https: //www. w3schools.com/howto/img_avatar2.png")。)
const lifter2 = Lifter. createWithDefaultBio("3"/span>,"Manfred"/span>, 73,107,125,220, "https: //www. w3schools.com/howto/img_avatar2.png")。)
const lifter3 = Lifter. createWithDefaultImageURL("3"/span>,"Manfred"/span>,73, 107,125,220,"sampleBio")。)
const lifter4 = Lifter. createWithDefaultBioAndImageURL("3"/span>,"Manfred"/span>, 73,107,125,220)。)
- 構建模式
它可以用幾種不同的方式來實作,下面只是一個示例,但你也可以用一些方法來修改它,例如,Lifter 類可以在建構式中只接受 LifterBuilder 作為引數,并從它那里獲取引數 - 這樣你就可以防止在沒有構建器的情況下初始化這個類。 具體的實作方式取決于你的需求和偏好。
export class Lifter {
id: string;
name: string;
weightclass: 數字。
bestSnatch: 數字。
bestCJ: number;
bestTotal: 數字。
bio: string = "默認傳記文本"。
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
constructor(id: string, name: string, weightclass: number, bestSnatch: number, bestCJ: number, bestTotal: number, bio: string, imageURL: string) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
}
export class LifterBuilder{
id: string;
name: string;
weightclass: 數字。
bestSnatch: 數字。
bestCJ: number;
bestTotal: 數字。
bio: string = "默認傳記文本"。
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
constructor(id: string, name: string, weightclass: number, bestSnatch: number, bestCJ: number, bestTotal: number) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
withBio(bio: string)。LifterBuilder {
this.bio = bio。
return this。
}
withImageURL(imageURL: string)。LifterBuilder {
this.imageURL = imageURL;
return this。
}
build()。Lifter {
return new Lifter(this. id, this. name, this.weightclass, this。 bestSnatch, this.bestCJ, this。 bestTotal, this.bio, this.imageURL)。)
}
}
const lifter = new LifterBuilder(. ...).withBio(...).withImageURL(. ...).build(); //只是在需要時呼叫withBio或withImageURL,如果你不想改變默認值就不要呼叫任何一個。
uj5u.com熱心網友回復:
bio?: string;
imageURL?: string;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/323553.html
標籤:
上一篇:未發現引數為'(',)的''反轉。嘗試了1種模式。['accConnect/setting/(?P<settings_pk>[0-9] )
