Hi guys,
Just a quick question, I'm working on a general stack class for use with my card game and rather than creating an overloaded constructor for every single object and primitive type I'm wondering is there a way to supply the data type when invoking a basic constructor?
Basically what I'm asking is instead of
public class Stack
{
public Stack(String any, int size)
{
String[] stackOfStrings = new String[size];
}
public Stack{int any, int size)
{
int[] stackOfInts = new int[size];
}
//etcetera through all primitive and object types
}
Is there a quick way to create a constructor, or any method for that matter, where the type of data structure created is decided when the method is called? I know I could have the constructor take a string and have a number of cases for each word so that I could call Stack("int") and so forth. I'm just wondering if there's any nice tricks out there for cutting down on having to write 10 or so overloaded constructors, after all I'm told programmers hate doing anything in 2 steps where half a step will do :)