How to get ArrayList extends class name ?

Hello, i have a ask, how to get entity extends class name ?

ArrayList<Entity> entities;
void setup() {
  size(500, 500);
  entities = new ArrayList <Entity>();
  entities.add(new Sign("Hello!", 200, 64));
  entities.add(new Sign("test", 200, 128));
}
void draw() {
  background(0);
  println(entities.get(0).xPos);
  ((Sign) entities.get(0)).text = "testChangeText";
  println( ((Sign) entities.get(0)).text);
  for (Entity entity : entities) {
    entity.show();
    // how to get if a entity SIGN or BLOCK or some.. ? <----
    entity.handle();
  }
}

 public class Layer extends Sign {
   Layer(){
     super("woah",200,200);
   }
 }


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

  • Koogs, its working, but how to split to a String, only name of class?

    ArrayList<Entity> entities;
    void setup() {
      size(500, 500);
      entities = new ArrayList <Entity>();
      entities.add(new Sign("Hello!", 200, 64));
      entities.add(new Sign("test", 200, 128));
    }
    void draw() {
      background(0);
      //println(entities.get(0).xPos);
      ((Sign) entities.get(0)).text = "testChangeText";
      //println( ((Sign) entities.get(0)).text);
      for (Entity entity : entities) {
        entity.show();
        String className = entity.getClass().toString();
        String[] classNameArray = className.split("$");
        className = classNameArray[0]; // How to split ? <<<--- 
        println(className);
        entity.handle();
      }
    }
    
     public class Layer extends Sign {
       Layer(){
         super("woah",200,200);
       }
     }
    
    
    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();
    }
    
  • i don't know. (actually, i do)

    i'm only tidying up after you after you started a new thread for the same code as before.

  • its working, but how to split to a String, only name of class?

    Please provide minimum amount of code demonstrating the part you are struggling with. How much of the code you posted is relevant to your issue? Please review what an MCVE is.

    Kf

  • edited March 2018 Answer ✓

    @GeorgeJava -- did you look at the links @GoToLoop provided about instanceOf()?

    // how to get if a entity SIGN or BLOCK or some.. ? <----
    

    Like:

    And:

    So:

    if (entity instanceof Sign)
      // foo 
    }
    if (entity instanceof Block)
      // bar
    }
    
Sign In or Register to comment.