lundi 22 mai 2017

How can I access a class's object by a name stored in a String in java [on hold]

My Code is:

class A{
public void Say();
}

class B{
public void Say();
}

class C{
public void Say();
}

class D{
A x = new A();
B y = new B();
C z = new C();
public void A_Say(){
x.Say();
}
public void B_Say(){
y.Say();
}
public void C_Say(){
z.Say();
}
}

class E{
D d = new D();
String string; //string is between A , B , C and input from console
D.class.getMethod(string+"_Say").invoke(d);
}

and I don't want it I want to access Say() function directly from" class E "by reflection without switch-case can I?

or I want to access a object of a class that have been created by its name and call a function that is in it. please help me.





Aucun commentaire:

Enregistrer un commentaire