簡體   English   中英

從同一父級的另一個 class 訪問 object

[英]Accessing object from another class of the same parent

我正在將之前在 JAVA 上編寫的面向對象的代碼傳輸到 dart 以通過 flutter 框架在我的設備上對其進行視覺測試。
我有一個名為Person的 class ,它有兩個子類SuperPerson (有兩個子類SuperHeroVillian並具有攻擊方法)和Civil 我為SuperHero class 做了一個方法,稱為保護,旨在保護對象免受民用class 的傷害。

現在,我正試圖做到這一點,當VillianSuperHero的 object 上多次調用Attack方法時,該對象的生命值為零,這使得SuperHero 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);
    }
  }

一個超級英雄可以保護多個平民嗎? 如果是這樣,將 SuperHero 當前正在保護的公民列表 ( 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 中,然后您可以在需要刪除保護關系時訪問它。 總體而言,您的代碼外觀可以通過僅引用有問題的對象而不是使用字符串和布爾值來改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM