Create a new object of a subclass with a String

I recently saw a similar problem here

The problems and solutions are described in the link above. If you adapt it to your code, it becomes

interface IMenuRunnable {
}

abstract class Item implements IMenuRunnable {
  PVector Start;
  PVector End;
  float Progress;

  boolean delete;

  boolean flammable;
  int BurnNumbers;

  Item(float Speed, PVector Pos) {
    Start = Pos;
    End = Pos;
    Progress = 0f;

    delete = false;

    flammable = false;
    BurnNumbers = 0;
  }
}
class Iron extends Item {
  Iron(PVector Pos) {
    super(1f, Pos);
  }
  Iron() {
    super(1f, new PVector());
  }
}
class Gold extends Item {
  Gold(PVector Pos) {
    super(1f, Pos);
  }
  Gold() {
    super(1f, new PVector());
  }
}
class Copper extends Item {
  Copper(PVector Pos) {
    super(1f, Pos);
  }
  Copper() {
    super(1f, new PVector());
  }
}
class Coal extends Item {
  Coal(PVector Pos) {
    super(1f, Pos);
    flammable = true;
    BurnNumbers = 5;
  }
  Coal() {
    super(1f, new PVector());
  }
}

final String THISPROC = this.getClass().getCanonicalName();

Item getItem(String Name) {
  Item item = null;
  try {
    Class<?> sketchClass = Class.forName(THISPROC);
    Class<?> c = Class.forName(THISPROC + "$" + Name);
    java.lang.reflect.Constructor constructor = c.getDeclaredConstructor(sketchClass);
    item = (Item)constructor.newInstance(this);
  } 
  catch (ClassNotFoundException e) {
    println(e);
  } 
  catch (Exception e) {
    println(e);
  }
  return item;
}

void setup() {
  println(new Iron());
  println(getItem("Iron"));
  println(new Coal());
  println(getItem("Coal"));
}

However I’m not sure if I recommend to do so :slight_smile: Your switch statement looks a bit wordy or repetitive, but it’s a good way to keep track of what is going on.

3 Likes