Using a string when creating an object.
Say I have a list of plants and a bunch of plant<Plantname> classes.
I want to be able to use a plant from the array to create an object for referencing a plant<Plantname> class.
My first class.
Code :
public class plantList{
public static void main(String[] args){
String[] plantList ={"Tree","Bush","NotherPlant","NotherNotherPlant"};
}
}
My other classes go along these lines:
Code :
public class plantTree{
String Color=Brown,OtherStuff=DescriptionOfOtherStuff;
}
I wish it was as simple as putting:
Code :
plant(plantList[/*number*/]) goplant = new plant(plantList[/*number*/])
This is obviously not the actual stuff I would put in here, but just an example.
How would I go about doing this?
Re: Using a string when creating an object.
Quote:
I want to be able to use a plant from the array to create an object
Plant[] somePlants = new Plant[100]; // Create an array of Plant
...
somePlants[i] = new Plant(...); // put some Plant objects into the array
...
NewObj newObj = new NewObj(somePlants[y]); // create a new object using an element from the array
Re: Using a string when creating an object.
But the string from the array needs to be in the object after plant.
So if I have plantList[0] as tree and I'm making the new object to get info from plantTree.java, I need the computer to read it as:
plantTree goplant = new plantTree;
where the Tree in plantTree is pulled from the array.
This is so if I want to access plantTree, or any other plant<plantname>, from main (for getting variables to print and such), I would just have the new object use the array.
Re: Using a string when creating an object.
Are you asking how to create a specific object based upon a String value? Personally I dislike this algorithm but you can achieve it using a if/else if statement.
Code java:
if string is tree {
create plantTree object
} else if string is bush {
create plantBush object
} etc
Re: Using a string when creating an object.
Your description of your problem is confusing.
Quote:
string from the array needs to be in the object after plant.
What does "the object after plant" mean? Can you give an example
Quote:
plantList[0] as tree and I'm making the new object to get info from plantTree.java
Does the plantList array contain tree objects? Is tree a class?
How does the plantTree.java source file relate to this?
Quote:
where the Tree in plantTree is pulled from the array.
Your changing case is confusing. Is Tree a class and is it the same as tree?
Re: Using a string when creating an object.
1st question:
The object after plant means that I want to have the word plant (the prefix for all my plant<plantname> classes) to have the value of the string directly after it.
ie, I have:
plantTree goplant = new plantTree
but I want to be able to exchange tree with any other string.
sort of like: (this doesn't work but it gets my point across)
plant<followed directly by>plantList[<number here>] goplant = new plant<followed by>plantList[<number>]
2nd question:
The plantList array contains Tree as a string. (in my example at plantList[0])
plantTree is a separate class that needs to be accessed by main. (for retrieving variables and such)
plantTree,java is the .java file for the class plantTree.
3rd question:
It sort of is, but it's actually plantTree.
The lowercase Tree is a typo, it should all be the same Tree with a capital "T".
Sorry about this, I'm terrible at asking for help and explaining things to people.
Re: Using a string when creating an object.
Quote:
object after plant means that I want to have the word plant (the prefix for all my plant<plantname> classes) to have the value of the string directly after it.
ie, I have:
plantTree goplant = new plantTree
but I want to be able to exchange tree with any other string.
That sounds like a macro or preprocessor activity. Java does not have that.
You will have to explicity code all your class definitions giving them each a unique name as you wish.
Class names in java should all start with capital letters by convention.
The class names should be like PlantTree.
Quote:
The plantList array contains Tree as a string.
Let me reword that. The String array named plantList contains the String "Tree".
For example:
String[] plantList = new String[] {"Tree"}; // define an array containing the String "Tree"
Quote:
plantTree is a separate class that needs to be accessed by main
Naming conventions require that the class name be: PlantTree
I assume by main you mean the main() method. Where will you put the definition of the PlantTree object variable? There can be problems defining a variable in a method. It will only be known in that method.
Re: Using a string when creating an object.
Darn, I plan to have several hundred "plant<plantname>" classes.
I felt like being lazy and not making a VERY long else if...
All that is contained in the "plant<plantname>" classes is several variables.
I just wanted to relay that info from the classes to main.
Re: Using a string when creating an object.
Then you need to think of a better design. I said I disliked the suggested solution because it stinks. Creating objects based upon some String value is often asked about by n00bs and usually the wrong way to solve the problem.
Re: Using a string when creating an object.
Quote:
All that is contained in the "plant<plantname>" classes is several variables.
What are the values you want to save?
Perhaps one class can contain the data for each plant.
Then use a Map to hold the objects for each type of Plant.
The key would be the plant name and the value would be the object with the values.
Re: Using a string when creating an object.
Then you need to think of a better design. I said I disliked the suggested solution because it stinks. Creating objects based upon some String value is often asked about by n00bs and usually the wrong way to solve the problem.