java 리셋 코드 구현 실례
S - Windows와 X Windows 이벤트 구동 디자인 모델에 익숙한 개발자는 일반적으로 하나의 방법의 지침을 이벤트 원본에 전달하고 어떤 사건이 발생할 때 이 방법을 호출한다.Java의 대상을 대상으로 하는 모델은 현재 방법 지침을 지원하지 않기 때문에 이런 편리한 메커니즘을 사용할 수 없을 것 같다.
Java는 인터페이스를 지원하며 인터페이스를 통해 같은 리셋을 할 수 있습니다.그 비결은 간단한 인터페이스를 정의하고 리셋을 원하는 방법을 설명하는 데 있다.
예를 들어 어떤 사건이 발생할 때 알림을 받을 수 있다고 가정하면 우리는 인터페이스를 정의할 수 있다.
public interface InterestingEvent {
// , 、
public void interestingEvent();
}
이렇게 하면 우리는 이 인터페이스 클래스의 대상을 실현하는 손잡이grip을 가질 수 있다.이벤트가 발생하면 Interesting Event 인터페이스를 실현하는 대상을 알려주고 interesting Event () 방법을 호출해야 합니다.
class EventNotifier {
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier(InterestingEvent event) {
ie = event;
somethingHappened = false;
}
public void doWork() {
if (somethingHappened) {
// ,
ie.interestingEvent();
}
}
}
이 예에서 이벤트가 발생했는지 여부를 somethingHappened로 표시합니다.이벤트 알림을 받기를 원하는 클래스는 Interesting Event 인터페이스를 실현하고 자신의 인용을 이벤트 알림자에게 전달해야 합니다.
public class CallMe implements InterestingEvent {
private EventNotifier en;
public CallMe() {
// ,
en = new EventNotifier(this);
}
// ,
public void interestingEvent() {
// ,
}
}
이상은 매우 간단한 예를 통해 자바의 리셋의 실현을 설명한다.물론 이벤트 관리나 이벤트 알림자 클래스에서 등록하는 방식으로 이 이벤트에 관심 있는 여러 개의 대상을 등록할 수 있다.
1. 인터페이스 Interesting Event를 정의하고 리셋 방법인nteresting Event(String event)는 String 매개 변수를 간단하게 수신합니다.
interface InterestingEvent {
public void interestingEvent(String event);
}
2. Interesting Event 인터페이스, 이벤트 처리 클래스 구현
class CallMe implements InterestingEvent {
private String name;
public CallMe(String name){
this.name = name;
}
public void interestingEvent(String event) {
System.out.println(name + ":[" +event + "] happened");
}
}
3. 이벤트 관리자 또는 이벤트 알림자
class EventNotifier {
private List<CallMe> callMes = new ArrayList<CallMe>();
public void regist(CallMe callMe){
callMes.add(callMe);
}
public void doWork(){
for(CallMe callMe: callMes) {
callMe.interestingEvent("sample event");
}
}
}
4. 테스트
public class CallMeTest {
public static void main(String[] args) {
EventNotifier ren = new EventNotifier();
CallMe a = new CallMe("CallMe A");
CallMe b = new CallMe("CallMe B");
// regiest
ren.regist(a);
ren.regist(b);
// test
ren.doWork();
}
}
이상은 자바 리셋 메커니즘에 대한 소개입니다. 필요한 학생은 참고하시기 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.