我想弄清楚如何在Swift中複製一個示例Java公共接口。這是我想到的。你會如何在Swift中複製這個Java公共接口?
下面是一個例子的Java公共接口:
public interface TestInterface {
public static int MSG_1 = 0x11;
public static int MSG_2 = 0x22;
public static int MSG_3 = 0x33;
public int getMessageType();
}
然後在一個新的類將實施的TestInterface,例如:
import com.myTestApp.TestInterface;
public class msg1Type implements TestInterface {
public int getMessageType() {
return MSG_1;
}
}
在斯威夫特我已經做了如下 - 添加一個名爲TestInterface.swif的文件:
import Foundation
class TestInterface {
let MSG_1 :Int = 0x11
let MSG_2 :Int = 0x22
let MSG_3 :Int = 0x33
let getMessageType((void)->Int)
}
然後在實現Tes的類tInterface的getMessageType()函數,如:
import Foundation
class msg1Type : TestInterface {
func getMessageType() -> Int {
return MSG_1;
}
}
什麼是最好的方法來實現這個實驗?
你的代碼出現,以反映基本斯威夫特語言概念,包括協議,初始化,甚至函數簽名缺乏瞭解。有人可能會提供正確的答案,但我強烈建議您閱讀Apple的官方Swift書籍,並節省大量不必要的試驗和錯誤。 – mattt 2014-12-07 00:33:55
@Matt - 謝謝,實際上我會花一些時間閱讀,而不是試圖學習SWIFT的蠻力。我喜歡使用其他語言的示例,然後移植,以便了解兩者之間的差異。也許不是最好的方法。 – Mavro 2014-12-07 00:56:55