vendredi 13 mars 2015

How to call method from a dynamic type of Object inside a class?



What I want to do:


Calling some method which existed in more than one class, while substance is passed into another class.


What I am doing :


I have several Class that extends from JPanel, and all of them have common method alert(), reflesh().



public class MyView01 extends JPanel{

public void alert(String msg){
// do somthing...
}
public void refresh(){
// do some other thing...
}

}

public class MyView02 extends JPanel{

public void alert(String msg){
// do somthing...
}
public void refresh(){
// do some other thing...
}

}


Then I have a Class for action which will pass substance of my Class into it, for calling method from my Class.



public class MyViewAction{

private Object view;

public void setView(Object view){
this.view = view;
}

public void someOperating(){

int result = doSomthingElse();
if(result == 1){
view.refresh();
}else{
view.alert("The answer is 42.");
}
}

}


While someOperating() may using in both class MyView01, MyView02, I want to using this MyViewAction Class to declare it (DRY). I want to passing difference type of Object into the view field, to do so, I try "Object" type, however I cant call the method in this way.


What should I change to make it working ?




Aucun commentaire:

Enregistrer un commentaire