Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
September 26th, 2008, 09:22 AM
|
#1 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 5
| variable classes but same method
In JAVA I have 26 classes named A, B,.......,Y,Z. Each of them implements a method named Meth().
I have a String str that can contain the letter "A" or "B" or ....... or "Y" or "Z".
How can I implement the statement str.Meth();
so it will go to a different method depending on the value in str.
Thank you |
| |
September 26th, 2008, 12:32 PM
|
#2 (permalink)
| | Go back to sleep
Join Date: Jul 2002 Location: Switzerland
Posts: 6,052
|
so the methods do different things? or are the really the same just in different classes which seems to be kind of strange
could you state your problem, maybe there is a simpler or other way of solving it
Creatures
__________________
Knowledge Is Power
|
| |
September 26th, 2008, 03:25 PM
|
#3 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 5
|
The methods do different things depending on the class. I could use an IF ELSE construct and just list them all or use a SWITCH CASE construct but I am trying to do it in one statement. |
| |
September 26th, 2008, 04:53 PM
|
#4 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,540
|
So essentially you have Class1, Class2, Class3 all of which implement Meth()
You have a string that contains the name of a class.
So str = "Class3";
You want to turn something like Code: switch(str){
case "Class1":
Class1.Meth();
} Into a one line statement in Java?
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
September 26th, 2008, 05:02 PM
|
#5 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 5
| |
| |
September 26th, 2008, 05:12 PM
|
#6 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,540
| |
| |
September 27th, 2008, 12:07 AM
|
#7 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 5
| Sad but it didnt work.
When I tried
Object anObject = Class.forName("AK").newInstance();
anObject.Transform();
the compiler said "The method Transform() is undefined for the type Object ".
When I tried
((AK) anObject).Transform();
it compiled
When I tried
Class aClass = (Class) Class.forName("AK").newInstance();
aClass.Transform();
the compiler said "The method Transform() is undefined for the type Class".
When I tried
AK.Transform(); it compiled.
For some reason the class name must be mentioned. |
| |
September 27th, 2008, 10:32 AM
|
#8 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,741
|
Not 100% sure what you're trying to do??? So if str = A or B, you want to call A.Meth()???
Hmmm...seems to me when you instantiate the class, you'll have access to the correct Meth(); function; I just don't know what you're trying to do LOL
void main() {
A myClassA;
myClassA.Meth();
B myClassB;
myClassB.Meth();
// do you want this?
if(str == 'A')
myClassA.Meth();
} |
| |
September 27th, 2008, 10:26 PM
|
#9 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 5
| Solution
To all those who gave suggestions a hearty thank you. The final solution was a follows:
An interface was set up.
************************************
package writeTextFile;
public interface WriteFile {
public void Transform();
}
************************************
All the classes implement the interface such as:
************************************
package writeTextFile;
public class AK implements WriteFile
{
public void Transform()
{
System.out.println("Went to AK");
return;
}
}
************************************************** ********
The 2 statements necessary to execute the transfer were:
************************************************** ************
WriteFile t = (WriteFile) Class.forName("writeTextFile." + state).newInstance();
t.Transform();
************************************************** ************
where the string state contained the 2 character class name.
The important but not obvious point was that the package name "writeTextFile" had to be included.
Thank you again for all your help. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |