- 自訂型別可以實現內建 interface 來擴充自己的功能。
- interface 只是一群 abstract members 的集合。interface 表達一種 behavior 可以讓 class 或 structure 選擇去 implement 這樣的 behavior。
- interface 當中只有 property 和 method 成員,而且所有成員都是 abstract。
Ref:
http://wiki.developers.facebook.com/index.php/Stream_filter_(FQL)
- uid
- uid in the where clause must be the session user
- filter_key
- name
- rank
- icon_url
- 目前拿來分類 status, link, photo 的賤招
- is_visible
- type
- The type of filter. One of application, newsfeed, friendlist, network, or publicprofile.
- value
- 當 base class 定義了一個 public method,所有的子類別分支(相關型別) 對於這個方法的反應都是一樣的。如何使相關型別對於同一個 request 做出不同的 respond 就是多形所要探討的。
- virtual and override Keywords
- 若一 base class 希望當中定義的 method 可以(may be)被覆寫,那麼就要使用 "virtual";若一子類別要覆寫 base class 的 virtual method 的話,則要加上 "override"。
- 在 overriden method 當中不一定要全部重新定義,可以透過 "base" 來存取原 base class 的功能。
- 有時候只是想把 class 當中的某個 virtual method 給 seal 起來,讓子類別無法進行覆寫,而非對整個 class seal。這時候可以針對 virtual method 下 "sealed" Keyword
- base class 通常定義一般性的物件元素,創建 base class 的物件通常不具有太大的意義,因此可以透過 "abstract" 來避免針對 base class 進行物件的創建。
- 在 abstract class 當中,可以定義 abstract member,這種 member 可以完全不定義其 default implementation (不同於 viutual)。因此你強迫了每一個繼承此類別的子類別自行定義該方法之實做。
- 假若子類別定義了一個和父類別一模一樣的成員,那麼子類別的版本會覆蓋父類別的版本。
- 當你從外部取得一個類別,而要將此外部類別融入你的 base class 的時候,就可能會發生 member shadowing 的情形。這時候會得到一個 warning,有兩種解決方式。一、返回 base class 將被 shadow 的 method 加上 "virtual"。二、針對該子類別的 shadowing method 加上 "new",意味著覆蓋所有在此之前的版本。
- Base Class/Derived Class Casting Rules
- 用 base class type 宣告的陣列當中可以放其子類別創建的物件(implicit cast)。it is always safe to store a derived type within a base class reference (e.g. baseclass A = new derived ();) 。
- 這麼做的好處是當你定義一個方法的輸入參數為 base class reference,那麼你就可以傳遞所有 derived class 的物件到這個方法當中。
- 除了 implicit cast 還可以直接用 explicit cast 硬轉,但是不是每種狀況都可以硬轉,C# 提供 "as" Keyword 讓你檢查兩個物件的 compatability。
- Hexagon hex = (Hexagon)frank;
Hexagon hex2 = frank as Hexagon;
if (hex2 == null)
Console.WriteLine("Sorry, frank is not a Hexagon...");
- 第二點當中的方法要怎麼知道丟進來的物件是哪一個 derived type 來做相對應的處理(知道是哪一個derived type 之後再用 explicit cast) 呢? C# 提供 "is" Keyword 和 "as" 相似,只是如果兩個物件 incompatable 的話 "as" 會返回 null, "is" 會返回 false。
- code reuse 有兩個面向: 一種是 is-a ( classical inheritance) 關係、一種是 has-a (containment/delegation) 關係。
- 所謂 has-a 關係允許一個類別定義一個其他類別的成員變數,並且把該成員變數的 functionality 間接的 expose 給該類別使用者。這也達到封裝 (data hiding) 的目的,因為如此一來該物件使用者並不知道該類別中還包含了另一個類別成員變數。
- 子類別若定義新的成員,該成員並不能存取父類別的 private member。
- C# 當中沒有多重繼承,一個類別只能繼承自單一父類別。但是 interface 則可以 derive frome multiple interfaces。
- 使該 class 無法被繼承
- C# structures 隱含預設為 sealed 即無法被繼承
- 子類別建構子當中的參數,如果是要拿來初始化父類別的欄位資料的話,需要使用 "base" 來增加初始化的效率。
- public Manager(string fullName, int age, int empID, float currPay, string ssn, int numbOfOpts) : base(fullName, age, empID, currPay, ssn) 例如:前五個參數主要拿來初始化繼承而來的 field data,因此用 "base" 明確指定對應到哪個欄位
- 使用 "protected" 的好處是: 子類別要存取父類別當中的 protected 成員不再需要透過 public 的 properties,而可以直接存取。當然這會造成一些封裝的危險,即可以透過子類別改變父類別的 internal data。Although protected field data can break encapsulation,it is quite safe (and useful) to define protected methods.When building class hierarchies,it is very common to define a set of methods that are only for use by derived types
- protected 成員在非家族類別的眼中即為 private。
- 在繼承分支到最後,一些子類別在分支下去也許沒有太大的意義,這時候可以利用 "sealed" Keyword 避免該類別再一步被 extend,也就是無法被繼承。
- 要描述 has-a 的關係很簡單,只要在一個類別 A 當中宣告另一個類別的物件 B,即能表達 A contains B 的概念。所謂 Delegation 指的則是在 A 中創建一個方法,讓使用者可以透過者個透露在外頭的 A 的方法,而完成 B 所能達到的功能性。
- 巢狀型別允許對於 inner type 得到完全的 accessru,即使被宣告成 private。
- 因為 inner type 屬於 outer class 的一個成員,因此他可以存取 outer class 的 private 成員。
- inner type 經常扮演 outer class 的 helper,而並非為外部所用。
- 型別成員的隱含預設為 private, 型別的隱含預設為 internal
- Internal items are accessible only within the current assembly. Therefore, if you define a set of internal types within a .NET class library, other assemblies are not able to make use of them.
- 希望類別創建的物件可以讓外部 assembly 使用就要把 class 加上 public,若希望類別當中的成員可以讓其他類別使用,也要將類別成員加上 public 。
- 一般 class 不能加上 private,但是巢狀型別 (class 當中包含的別的型別的成員變數) 可以。
- Properties make your types easier to manipulate, in that properties are able to respond to the intrinsic operators of C#。
- A property really maps to a get_/set_ pair! 因此不能在類別當中在宣告 get_xxx, set_xxx 的方法
- 在 get 或 set 之前加上 accessibility keyword (e.g. "protected")。
- Read-Only / Write-Only 的 Property: 拿掉 get 或 set 其中一個即可。
- The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item
- 利用 const 這個 keyword 定義在初始化之後其值不在改變的資料。
- const field 隱含為 static,可以直接透過類別存取。
- constant data 在 compile 的時候就必須知道該值,因此 const data 在宣告之後一定要給定初值。
- Read-Only Fields (勿與 read-only property 混淆)
- 剛好跟 const 不同,read-only field 可以不必在宣告之後馬上給定初值,因此可以透過建構子來指定初值 (讀取外部資料時有用)。
- read-only field 並非隱含 static,如果設定成 class 層級,要加上 "static"。
- Partial Types
- 允許你在多個 .cs 檔案中定義型別 (e.g. 可以將 properties 和 constructor 放在一個 .cs 檔,field data 和 type method 放在另外一個 .cs 檔)
- Invoked directly from the class level.
- 沒有必要透過創建一個物件來 invoke 的成員,通常會宣告成 static member,,static member 經常出現在 utility class 當中。
- 靜態成員只能操作其類別當中的靜態資料或靜態方法,不能操作非靜態成員。
- 類別當中的靜態資料在記憶體中只佔一個位置,所有的物件都指向該資料
- static methods can operate only on static data. However, a nonstatic method can
make use of both static and nonstatic data... 非靜態方法可以 存取/修改 靜態和非靜態資料,可以用來實現單一個物件對靜態資料進行修改,進而同步到每一個物件當中。
- 靜態建構子: 若類別建構子會重設靜態資料,那麼靜態資料在每一次的物件創見都會被重新設定一次。雖然說你可以在宣告靜態資料的時候就給定初值,但是有時候靜態資料的值是由資料庫或外部檔案所決定,因此有靜態建構子。
- a static constructor is a special constructor that is an ideal place to initialize the values of static data when the value is not known at compile time
- The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller... 在物件創建之前或者直接存取類別成員
- The static constructor executes before any instance-level constructors
- 靜態類別: 當類別被宣告成靜態,便無法用 new 創建物件,也只能包含靜態欄位和靜態成員。