拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 从同一个父类的另一个类访问物件

从同一个父类的另一个类访问物件

白鹭 - 2022-02-10 1961 0 0

我正在将之前在 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 中,然后您可以随时访问它,以便洗掉保护关系。总体而言,您的代码外观可以通过仅参考相关物件而不是使用字符串和布林值来改进。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *