我正在將之前在 JAVA 上撰寫的面向物件的代碼轉移到 dart,以通過 flutter 框架在我的設備上對其進行可視化測驗。
我有一個叫做類人是具有兩個子類SuperPerson(即具有兩個子類超級英雄和惡棍,并具有攻擊法)和民用。我為SuperHero類創建了一個方法,稱為保護,旨在保護物件免受Civil類的影響。
現在,當惡棍召喚進攻時,我正在努力做到這一點多次在SuperHero的物件上執行多次方法,直到該物件的健康狀況變為零,這會使SuperHero物件不再保護Civil。而且我不太確定如何訪問Civil的物件,以便我可以在死后使它們不受SuperHero物件的保護。
人物類
class Person {
//civil side
String protector = '';
bool protection = false;
//hero side
String protectedTargetName = '';
bool protecting = false;
//setters
//Civil side
String setProtector(String protector) {
return this.protector = protector;
}
bool setCivilProtection(bool protection) {
return this.protection = protection;
}
//Hero Side
String setProtectedTargetName(String protectedTargetName) {
return this.protectedTargetName = protectedTargetName;
}
bool setHeroProtection(bool protecting) {
return this.protecting = protecting;
}
//getters
//civil side
String getProtector() {
return protector;
}
bool isProtected() {
return protection;
}
//hero side
String getProtectedTargetName() {
return protectedTargetName;
}
bool isProtecting() {
return protecting;
}
}
超級英雄班
class SuperHero extends SuperPerson
{
SuperHero(String n, int a, String s, String p) : super(n, a, s, p) {
setProtectedTargetName('none');
setHeroProtection(false);
}
void toProtect(Person target, BuildContext context) {
String tName = target.getName();
String pName = getName();
setProtectedTargetName(target.getName());//Hero's side
setHeroProtection(true);//Hero's side
target.setCivilProtection(true);//Civil's side
target.setProtector(getName());//Civil's side
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text('$tName is under $pName\'s protection.'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
void toUnProtect(Person target, BuildContext context) {
String tName = target.getName();
String pName = getName();
setProtectedTargetName('none');//Hero's side
setHeroProtection(false);//Hero's side
target.setCivilProtection(false);//Civil's side
target.setProtector('none');//Civil's side
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text('$tName is no longer under $pName\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
民事類
class Civil extends Person {
//Constructor
Civil(String n, int a, String s) : super(n, a, s) {
setProtector('None');
setCivilProtection(false);
}
}
反派類:這只能洗掉從保護超級英雄的一面,但仍有保護在民法的身邊,我不知道如何從這里訪問它。
void attack(Person target, BuildContext context) {
String tName = target.getName();
String tPronouns = target.pronouns();
String tProtector = target.getProtectorName();
if (!(target.isProtected())) {
super.attack(target, context);
if (target.isProtecting()) {
if (target.getHealth() == 0) {
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content:
Text('$tName is no longer under $tProtector\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
//Hero side
target.setProtectedName('none');
target.setHeroProtection(false);
//Supposed to be Civil side but idk how to do it properly
setCivilProtection(false);
setProtectorName('none');
}
}
} else {
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text(
'You can\'t attack $tName, $tPronouns is already under $tProtector\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
UPFATE
宣告一個負責存盤受保護的Civil的變數在 Java 中作業正常且沒有錯誤。然而,當我試圖將它轉移到 dart 時,我收到了一個錯誤,因為我對 dart 不太熟悉。
人物類
Person protectedPerson; //Error at this line
Person getProtectedPerson() {
return protectedPerson;
}
Person setProtectedPerson(Person protectedPerson) {
return this.protectedPerson = protectedPerson;
}
錯誤:
Non-nullable instance field 'protectedPerson' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
除了在我嘗試取消保護Civil時收到另一個錯誤之外。我不知道是否有任何其他方法可以使此函式接受 Null 值。
setProtectedPerson(null);
錯誤:
The argument type 'Null' can't be assigned to the parameter type 'Person'.
更新 - 解決方案
在型別后添加問號有助于解決所有錯誤。
Person? protectedPerson;
Person? getProtectedPerson() {
return protectedPerson;
}
Person? setProtectedPerson(Person? protectedPerson) {
return this.protectedPerson = protectedPerson;
}`
uj5u.com熱心網友回復:
一個超級英雄可以保護多個平民嗎?如果是,則將 SuperHero 當前保護的 Civils 串列 ( List<Civil>
) 作為屬性存盤。每當超級英雄死亡時,移除相應超級英雄串列中所有成員的保護者。
List<Civil> protectedCivils;
// Whenever you want to remove all of the SuperHero's (target) protection of its Civils.
target.removeProtectedCivils();
// What the method would look like
void removeProtectedCivils() {
for(Civil civil : protectedCivils) civil.removeProtection();
}
否則,如果 SuperHero 只能保護一個 Civil,則將當前受保護的 Civil 作為屬性存盤在 SuperHero 中,然后您可以隨時訪問它,以便洗掉保護關系。總體而言,您的代碼外觀可以通過僅參考相關物件而不是使用字串和布林值來改進。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/402200.html
上一篇:動態創建的同名類