與其他語言一樣,我們可以在initialization
期間創建對象並覆蓋對象中的方法。請幫助我我該怎麼辦?如何初始化一個對象並重寫目標c中的方法?
例如:
public class DemoInitAndOverride {
public void handleMessage(){}
}
而在另一個類
public class SampleClass {
public void doSomeThing(){
DemoInitAndOverride demo = new DemoInitAndOverride(){
@Override
public void handleMessage() {
// TODO Auto-generated method stub
super.handleMessage();
}
};
}
}
****EDIT:****
謝謝大家對可能的解決方案和建議。我認爲現在提供一些關於可以幫助您提供解決方案的要求的細節非常重要。
處理程序的概念與Android Framework類似,它使用處理程序在2個線程或2個方法之間傳遞消息。請參見下面的代碼示範:
UI類(此處的用戶點擊一個按鈕,一個請求被調度到使用處理器類處理程序)
此爲演示處理機
/**
*
* Used for thread to thread communication.
* Used for non UI to UI Thread communication.
*
*/
public class DemoHandler {
public void handleMessage(Messages message){}
final public void sendMessage(final Messages message){
//Calling thread is platform dependent and shall change based on the platform
new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
handleMessage(message);
}
}
});
}
}
這是簡單的消息類
public class Messages {
public Object myObject;
//other hash map (key, values) and get data put data etc
}
這是簡單的用戶接口類的演示代碼:
public class UIClass {
public UIClass(){
//INIT
}
void onClick(int id){
//Some Button is clicked:
//if id == sendParcel
//do
TransactionProcessor.getInstance().sendParcel(handler, "Objects");
}
DemoHandler handler = new DemoHandler(){
public void handleMessage(Messages message) {
//Inform the UI and Perform UI changes
//data is present in the messages
};
};
}
這是樣品事務處理器類
公共類TransactionProcessor {
public static TransactionProcessor getInstance(){
return new TransactionProcessor(); //for demonstration
}
//Various Transaction Methods which requires calling server using HTTP and process there responses:
public void sendParcel(final DemoHandler uiHander, String otherdetailsForParcel){
//INIT Code and logical code
//Logical Variables and URL generation
String computedURL = "abc.com/?blah";
DemoHandler serverConnectionHandler = new DemoHandler(){
@Override
public void handleMessage(Messages message) {
super.handleMessage(message);
//Process server response:
//create a new message for the UI thread and dispatch
Messages response = new Messages();
//add details to messages
//dispatch
uiHander.sendMessage(response);
}
};
new Thread(new ServerConnection(computedURL, serverConnectionHandler));
}
public void sendEmail(final DemoHandler uiHander, String otherdetailsForEmail){
//SAME AS SEND PARCEL WITH DIFFERENT URL CREATION AND RESPONSE VALIDATIONS
}
public void sendNotification(final DemoHandler uiHander, String otherdetailsForNotifications){
//SAME AS SEND PARCEL WITH DIFFERENT URL CREATION AND RESPONSE VALIDATIONS
}
}
卸下'【JAVA ],因爲答案與Java沒有任何關係。 –
謝謝,它是真的;答案與java無關 –
在Objective-C中,無論你何時子類化它都會被自動覆蓋,如果你想調用超類的方法,你需要調用[super method]; –