跳到主要內容

發表文章

匿名內部類別

在JAVA 8有新的語言特性出現,原本在interface都只限定宣告方法,不能有具體的實作。 但是現在已經可以在interfcace中實作方法,分別是static method與default method。 public interface NewFeature { static void produce() { System.out.println("N&F Vehicles"); } public default void linux() { System.out.println("LINUX"); } void botherm(); } public class App { public static void main(String[] args) { NewFeature.produce(); //直接使用 interface 的 static method new NewFeature() { //匿名內部類別寫法 @Override public void linux() { //覆寫 interface 的 default method, 此時不需再加上 default //因為只有一般類別才能建立實體, 因此一般類繼承了 interface 時, 覆寫 default method // 也不用加上 default, 反之界面繼承界面若要覆寫 default method時, 必需要加上 default System.out.println("new linux"); } @Override public void botherm() {} //直接在匿名類別裡實作界面方法 }.linux(); NewFeature intfaceInstance = new NewFeature() { //匿名內部類別寫法 @Override public void botherm() { System.out.println("匿名內部類別"); }}; intfaceInstance.lin...
最近的文章

json名稱開頭大小寫通吃

json syntax: {"name":"Andy"} java class: public class JsonModel { private String name; public void setName(String s) { this.name = s; } public String getName() { return this.name; } } 在java裡創建一個class, 用來接上面的json, 使之成為一個object供程式直接來使用, 這個過程稱反序列化 如果現在把上面那段json轉成java object時, 可用下面方式, 不用加任何Jackson annonation ObjectMapper jsonOmp = new ObjectMapper(); jsonOmp.readValue([json_string_source], JsonModel.class); 重點來了, json syntax中的name, 對於有些人來說, name與Name的意思是相同的, 但是這在做反序列化可行不通 若要兩者都可以吃下來, 則在class要多加annonation和method 例如: public class JsonModel { private String name; public void setName(String s) { this.name = s; } public String getName() { return this.name; } } 如果原本就是接受json name屬性是小寫, 突然有一天, 全部都要改大寫的話, 就直接在java class 屬性加上 @JsonProperties("Name") 如此一來, 就只能接受開頭大寫

Jackson - @JsonAnyGetter

這個註解是把Map資料型能屬性當成一般的資料型態屬性 舉例來說, json語法為name:value的組合, 而Map也是key:value的組合, 兩者很像 但是, Map是一種集合, 所以不加任何註解在Jackson解析下, 會保留屬性名稱, 而內容值則為Map的所有集合 public class JsonPojo {     public String name;     private Map properties;           @JsonAnyGetter(enabled = true)     public Map getProperties() {         return properties;     }          public void add(String k, String v) {         if (properties == null)             properties = new TreeMap ();                  properties.put(k, v);     } } Test: public class JsonPojoTest {     @Tes...

Jackson - @JsonGetter

用來表示一個json pojo model的某個屬性getter method是哪一個mothod 亦即, jackson解析時, 預設會先去找setter/getter method, 即便我們不需要給任何的annonation 意思是, 只要是get/set開頭的method, jackson都會掃描, 而預設屬性名稱為去掉get/set後, 第一個字母變小寫 ex. private String Name; public String getName() {return Name;}  -->屬性名稱為name public String getAbcd() {return Name;}  --> 屬性名稱為abcd 而且這兩個屬性都會被輸出 回到JsonGetter上面來, jackson都可以自己解析了, 何需用此註解 有一種情況會需要, 就是取得屬性的方法名稱不會命名為getter方式, 則用此註解來告訴Jackson, 這個方法需要輸出成json的一部份 ex. public String printName() {return Name;} --> Jackson parser時不會理會這個方法 而加上註解後 @JsonGetter("name") public String printName() {return Name;} -->json syntax is {"name" : xxxx} @JsonGetter() public String printName() {return Name;} -->json syntax is {" printName " : xxxx}

What Is A Kernel Module?

引述自  The Linux Kernel Module Programming Guide 原文 : What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.   自譯 :         Kernel module 其實就是一段程式碼,根據需求(service),module 可以載入 kernel 裡,或是從 kernel 中卸除,並不需要重新啟動作業系統(Linux),module 擴展了 kernel 的功能。         舉例,假設有一個 module 是屬於 device driver 類型,那麼這個 module 在 kernel 中可以直接存取與系統連線的硬體;相對地,若是 kernel 沒有提供 module 這樣的機制,我們必須在建置一個單體核心(ex. Linux)時,選擇直接加入新的功能到 kernel 裡,除此之外,kern...

在程式裡嵌入二進位檔(VC++)

Import: 1. 切換到 Resource Tab 2. (VC6)在專案名稱上按下 mouse 右鍵,選擇 "Import..." 2. (VC2005)在專案名稱上按下 mouse 右鍵,選擇 Add,再選擇 Resource,或是直接選擇 rc 按下右鍵,選擇 Add Resource,點下 "Import..." 3. 點選所要嵌入的檔案後,Resource Type 填入 RCDATA,存檔/編譯即完成。 解開: int ExtractFile(int nResourceId, LPCTSTR lpType, LPCTSTR strOutputName) {     DWORD dwWritten = 0;     HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(nResourceId), lpType);     if(NULL == hRsrc)     {         OutputDebugString(_T("FindResource fail !!!"));         return 1;     }     DWORD dwImageSize = SizeofResource(NULL, hRsrc);     if(dwImageSize == 0)     {         OutputDebugString(_T("FSize is 0 !!!"));         return 1;     }     HGLOBAL hBinImage = LoadResource(NU...

C-style String & C++ String & CString

C-style string to C++ string C-style string to CString char str[] = "Hello World"; string str = string(str); char str[] = "Hello World"; CString cs = CString(str); C++ string to C-style string CString to C-style string string str = string("Hello World"); char *ptr = str.c_str(); CString str = CString("Hello World"); char *ptr = str.GetBuffer(str.GetLength()); str.ReleaseBuffer();