class extends

Hello, my code not working, i dont know how to get the variable ... :/ can you tell me how ?

ArrayList<Entity> entities;
void setup() {
  size(500, 500);
  entities = new ArrayList <Entity>();
  entities.add(new Sign("Hello!", 200, 64));
}
void draw() {
  background(0);
  println(entity.get(0).Sign.text); // WHY ? HOW TO GET VARIABLE FROM SIGN?
  for (Entity entity : entities) {
    entity.show();
    entity.handle();
  }
}


public class Sign extends Entity {
  String text;
  public Sign(String text, int mapX, int mapY) {
    super(mapX, mapY, 14, 8);
    this.text = text;
  }
  void show() {
    noStroke();
    fill(80);
    rectMode(CORNER);
    rect(xPos, yPos, w, h);
    stroke(0);
    line(xPos + 2, yPos + 2, xPos + w - 3, yPos + 2);
    line(xPos + 2, yPos + 5, xPos + w - 3, yPos + 5);
  }

  void handle() {
      textAlign(CENTER, CENTER);
      fill(255);
      text(text, xPos, yPos-32);
  }
}

public abstract class Entity extends GameObject {
  public Entity(int xPos, int yPos, int w, int h) {
    this.xPos = xPos;
    this.yPos = yPos;
    this.w = w;
    this.h = h;
  }
  abstract void show();
  abstract void handle();
}

public abstract class GameObject {
  int xPos, yPos;
  int w, h; 
  abstract void show();
}

Answers

  • my code not working, i dont know how to get the variable ...

    Please be a bit clearer. What's the code doing wrong? Which variable?

    entity.get(0).Sign.text
    

    Your ArrayList is called 'entities' so that first bit should be entities.get(0). But this will be an Entity, not a Sign, so you won't be able to get the text field.

    Your ArrayList needs to be a list of Sign. Then you can use entities.get(0).text

  • bud text not existing i want to get the TEXT variable from Sign and print it or change it. How to make list of Sign ?

  • ArrayList<Sign> entities;
    
  • Prints duplicate :/

  • That's INSTEAD OF line 1.

    Change line 4 as well.

  • Like this ? But ERROR :/

    ArrayList<Entity> entities;
    void setup() {
      size(500, 500);
      ArrayList<Sign> entities;
      entities.add(new Sign("Hello!", 200, 64));
    }
    void draw() {
      background(0);
      println(entities.get(0).Sign.text); // WHY ? HOW TO GET VARIABLE FROM SIGN?
      for (Entity entity : entities) {
        entity.show();
        entity.handle();
      }
    }
    
    
    public class Sign extends Entity {
      String text;
      public Sign(String text, int mapX, int mapY) {
        super(mapX, mapY, 14, 8);
        this.text = text;
      }
      void show() {
        noStroke();
        fill(80);
        rectMode(CORNER);
        rect(xPos, yPos, w, h);
        stroke(0);
        line(xPos + 2, yPos + 2, xPos + w - 3, yPos + 2);
        line(xPos + 2, yPos + 5, xPos + w - 3, yPos + 5);
      }
    
      void handle() {
          textAlign(CENTER, CENTER);
          fill(255);
          text(text, xPos, yPos-32);
      }
    }
    
    public abstract class Entity extends GameObject {
      public Entity(int xPos, int yPos, int w, int h) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.w = w;
        this.h = h;
      }
      abstract void show();
      abstract void handle();
    }
    
    public abstract class GameObject {
      int xPos, yPos;
      int w, h; 
      abstract void show();
    }
    
  • ArrayList<Sign> entities;
    

    Instead of line 1.

    But I'm repeating myself...

  • Answer ✓

    Using your original code (the new code has more errors) change line 9 to

    println( ((Sign) entities.get(0)).text);

    This is NOT an ideal solution but it works.

    The problem is that your arraylist holds elements of type Entity, so when you use get(0) it returns an Entity object but text is a member of a child class so is not visible. What I have done is cast the Entity object to a Sign object so text is now visible.

    The problem with this approach is when you create new child classes from Entity. Assume you have created a class Notice that extends Entity and we add a Notice object to the arraylist. When this object is retrieved with get(...) it will cause an exception when we try to cast it to Sign.

    Remember a child class knows about its parent but the parent class knows nothing about any child class.

  • QUARK I LOVE YOU !!! <3 Thanks very much

  • Answer ✓

    BTW @koogs solution (working code shown below) not only works but is a better way to solve the problem.

    ArrayList<Sign> entities;
    void setup() {
      size(500, 500);
      entities = new ArrayList <Sign>();
      entities.add(new Sign("Hello!", 200, 64));
    }
    void draw() {
      background(0);
      println(entity.get(0).text); // Works now because it returns a Sign object
      for (Entity entity : entities) {
        entity.show();
        entity.handle();
      }
    }
    

    Casting an object from a collection to a child class is not a good idea, in fact Java introduced generics to provide a type safe way to use Java collections such as ArrayList.

    The only problem now is that new child classes of Entity (such as Notice) cannot be added to the array list.

    You need to think carefully about what classes you want and the relationships between them and then decide on how to store and access them.

This discussion has been closed.