从同一 parent 的另一个 class 访问 object
Accessing object from another class of the same parent
我正在将之前在 JAVA 上编写的面向 objected 的代码传输到 dart,以便通过 flutter 框架在我的设备上对其进行可视化测试。
我有一个叫 Person 的 class 有两个 child classes 是 SuperPerson(有两个 child classes 是 SuperHero 和 Villian 并且有一个 Attack Method)和Civil。我为 SuperHero class 制作了一个名为 protect 的方法,旨在保护 object 免受民用class。
现在,当 Villian 多次调用 Attack 方法时,我正试图做到这一点超级英雄的object到object的生命值归零的程度,这使得超级英雄 object 不再保护 Civil。而且我不太确定如何访问 Civil 的 object 以便我可以使它们不受 SuperHero object死后。
人Class
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
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
class Civil extends Person {
//Constructor
Civil(String n, int a, String s) : super(n, a, s) {
setProtector('None');
setCivilProtection(false);
}
}
Villian Class: 这里只是取消了SuperHero这边的保护,但仍然有保护Civil 的一面,我不知道如何从这里访问它。
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 不太熟悉。
人class
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;
}`
一个超级英雄可以保护多个公民吗?如果是这样,请将 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中,然后你可以随时解除保护关系。总的来说,只需引用有问题的对象而不是使用字符串和布尔值,您的代码看起来就可以得到改进。
我正在将之前在 JAVA 上编写的面向 objected 的代码传输到 dart,以便通过 flutter 框架在我的设备上对其进行可视化测试。
我有一个叫 Person 的 class 有两个 child classes 是 SuperPerson(有两个 child classes 是 SuperHero 和 Villian 并且有一个 Attack Method)和Civil。我为 SuperHero class 制作了一个名为 protect 的方法,旨在保护 object 免受民用class。
现在,当 Villian 多次调用 Attack 方法时,我正试图做到这一点超级英雄的object到object的生命值归零的程度,这使得超级英雄 object 不再保护 Civil。而且我不太确定如何访问 Civil 的 object 以便我可以使它们不受 SuperHero object死后。
人Class
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
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
class Civil extends Person {
//Constructor
Civil(String n, int a, String s) : super(n, a, s) {
setProtector('None');
setCivilProtection(false);
}
}
Villian Class: 这里只是取消了SuperHero这边的保护,但仍然有保护Civil 的一面,我不知道如何从这里访问它。
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 不太熟悉。
人class
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;
}`
一个超级英雄可以保护多个公民吗?如果是这样,请将 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中,然后你可以随时解除保护关系。总的来说,只需引用有问题的对象而不是使用字符串和布尔值,您的代码看起来就可以得到改进。