좋아, 내 문제에 대한 더 나은 제목을 생각할 수 없었다.메서드 호출자의 참조를 추가하는 방법은 무엇입니까?
나는 타이머 스레드가 있습니다. 그것은 틱하고 충분한 진드기가 있은 후에, 나는 그것을 호출 한 것에 대한 방법을 호출하기를 원한다. 만약 누군가 Unity를 사용했다면, 나는 이것을하려고합니다 :
public void test(String CallMeLater){
Invoke(CallMeLater); // Unity
caller.CallMeLater(); // Idea
}
그것은 정적이 아니라는 것을 주목할 가치가 있습니다. 어떻게해야합니까? 여기
내가 무엇을 가지고 :
CLOCK
public class Clock extends Thread {
Object test;
public Clock(int minutes, Object test) {
this.minutes = minutes;
this.test = test;
}
public void run() {
try {
Thread.sleep(1000);
} catch(Exception err) {}
test.SayHi();
run();
}
}
발신자
public class MainWindow {
// When the Clock is created i want to pass "this" into it.
app.Clock clock = new app.Clock(60, this);
public MainWindow(){
// Creates frame, and add listeners. I cut it out.
// It has nothing to do with the problem at hand
}
private void Set(){
// This is where i start the timer
clock.start();
}
public void SayHi(){
System.out.println("Hi");
}
}
콜백 메소드를 호출하고 싶습니다. 아마도 어떤 종류의 이벤트 알림을 제공하겠습니까? 그럼 원래 호출/타이머를 시작한 개체에 대한 참조가 필요합니다 ... – MadProgrammer
@MadProgrammer 그래. 그게 내가하고 싶은 걸. 그게 내가 어떻게해야하는지 묻는거야. 원래 그것을 시작한 객체를 어떻게 참조합니까? –
이 메소드에서 호출자를 인수로 전달하거나이 메소드의 호출자 유형으로 필드를 선언하십시오. –