次のシナリオでは、クラス B (同様に D、E、F など) のメソッド doSomething() を、それが使用されるクラス A のメソッドに置き換えたいと考えています。これについてどうすればいいですか?いくつかの例を作成しました。メッセージが伝わることを願っています
public class B implements GetNames{
public void getNameA(){ return "NameA"; }
public void getNameB() { return "NameB"; }
public void doStuff(){
//print names
doSomething(getNameA(), getNameB());
//print names
}
public void doSomething(String a, String b){}
}
public class A{
public void someMethod(){
B b = new B();
b.doStuff(); //*So I want it to call the method in B but somehow replace the doSomething method in B with the doSomething method in A
}
public void doSomething(String a, String b){
//print 'blabla' + a
//print 'blablabla' + b
//concatenate and print
}
}