• notice
  • Congratulations on the launch of the Sought Tech site

Accessing objects from another class of the same parent class

I'm in the process of transferring object-oriented code that I had previously written on JAVA to dart to pass the flutter framework Take a visual quiz on it on my device.
I have a class called Peopleis with two subclassesSuperPerson (i.e.has two subclasses Superhero and villain, and has Attacklaw) and civil.I amSuperHeroclass created a method, calledprotect , designed to protect objects fromCivilClass effects.
Now, when villainSummonattackMultiple timesSuperHero, until the object's health goes to zero, which makes SuperHeroObject is no longer protectedCivil.and I'm not quite sure how to access Civil objects so that I can after deathmake them immuneSuperHeroobject'sprotects.

Character

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;
      }
  }

Superhero 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);
  }
}

civil class

class Civil extends Person {
  //Constructor
  Civil(String n, int a, String s) : super(n, a, s) {
    setProtector('None');
    setCivilProtection(false);
  }
}

Villain class: This can only be washed off from protectionSuperhero side, but still protected in Civil Law's side, I don't know how to get from here visit it.

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
Declare a CivilVariables of works fine in Java with no errors.However, when I try to transfer it to dart, I get an error because I'm not very familiar with dart.
Character

Person protectedPerson; //Error at this line

  Person getProtectedPerson() {
    return protectedPerson;
  }

  Person setProtectedPerson(Person protectedPerson) {
    return this.protectedPerson=protectedPerson;
  }

Error:

 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'.

Except when I try to unprotectCivil.I don't know if there is any other way to make this function accept Null values.

setProtectedPerson(null);

Error:

The argument type 'Null' can't be assigned to the  parameter type 'Person'.

Update-Solution p>

Adding a question mark after the type helps resolve any errors.

Person? protectedPerson;

  Person? getProtectedPerson() {
    return protectedPerson;
  }

  Person? setProtectedPerson(Person? protectedPerson) {
    return this.protectedPerson=protectedPerson;
  }`

uj5u.com enthusiastic netizens replied:

Can one superhero protect multiple civilians? If so, list the Civils currently protected by SuperHero (List<Civil>) is saved as a property.Whenever a superhero dies, removes the protector of all members of the corresponding superhero string.

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();
}

Otherwise, if SuperHero can only protect one Civil, save the currently protected Civil as a property in the In SuperHero, you can then access it anytime to wash away the protective relationship.In general, the appearance of your code can be improved by only referencing related objects rather than using strings and booleans.

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+