下面重點說明面向對象的三個特征.繼承,封裝和多態. C#是一種現代的面向對象的語言. 繼承(inheritance):繼承是一個面向對象的詞語.說明,一個類(派生類)能分享,其它類(基類)的特征和行為.派
生類和基類是"is a"的關系. base classes(基類):通常基類可以自己實例化,或被繼承.派生類繼承基類中的成員,被標記為protected或更大
的權限.語法: class (derive class name):(base class name) 例子: //基類 public class Contact { //默認私有的字段 string name; string email; string address; //構造函數 public Contact() { // statements ... } //屬性 public string Name { get { return name; } set { name = value; } }
public string Email { get { return email; } set { email = value; } }
public string Address { get { return address; } set { address = value; } } } //派生類 public class Customer : Contact { //自己的私有字段 string gender; decimal income;
public Customer() { // statements ... } } 在上面的例子中,Customer 是
Contact的子類,不但,繼承了父類的成員,name,email,address;還有自己的成員,gender,income.
abstract classes(抽象類):抽象類是一種特殊的基類.除過普通的類成員,它們還有抽象的類成員.抽象類成員,
是不能被實例化的方法和屬性.所有直接從抽象類派生的類,必須實現抽象的方法和屬性.抽象類不能被實例化. 例子: //抽象類 abstract public class Contact { protected string name;
public Contact() { // statements... }
//抽象方法 public abstract void generateReport(); //抽象屬性 abstract public string Name { get; set; }}
public class Customer : Contact { string gender; decimal income; int numberOfVisits;
public Customer() { // statements }
public override void generateReport() { // unique report }
public override string Name { get { numberOfVisits++; return name; } set { name = value; numberOfVisits = 0; } } }
public class SiteOwner : Contact { int siteHits; string mySite;
public SiteOwner() { // statements... }
public override void generateReport() { // unique report }
public override string Name { get { siteHits++; return name; } set { name = value; siteHits = 0; } } } 上面的例子,定義了三個類.一個抽象類,兩個派生類.實現了父類的方法和屬性."override"修飾符,實現了抽象
類的方法. Calling Base Class Members(調用基類成員) 派生類能調用基類成員,如果,成員的修飾符是"protected"或更大權限.在適當的上下文條件下,好像調用自己的
成員一樣. 例子: abstract public class Contact { private string address; private string city; private string state; private string zip;
public string FullAddress() { string fullAddress = address + '\n' + city + ',' + state + ' ' + zip;
return fullAddress; } }
public class Customer : Contact { public string GenerateReport() { string fullAddress = FullAddress(); // do some other stuff... return fullAddress; } } 上面的例子中,派生類調用基類的方法:FullAddress(); 基類的構造函數,可以被派生類調用,用base(). 例子: abstract public class Contact { private string address;
public Contact(string address) { this.address = address; } }
public class Customer : Contact { public Customer(string address) : base(address) { } } 例子中,派生類沒有address成員,可以調用基類的構造函數. Hiding Base Class Members(隱藏基類成員) 派生類可以和基類有同樣名字的成員.這時,就會隱藏基類的成員. 例子: abstract public class Contact { private string address; private string city; private string state; private string zip;
public string FullAddress() { string fullAddress = address + '\n' + city + ',' + state + ' ' + zip;
return fullAddress; } }
public class SiteOwner : Contact { public string FullAddress() { string fullAddress;
// create an address... return fullAddress; } } 在例子中,派生類和基類有同樣的成員,FullAddress(),當調用時,基類的方法會被隱藏.
盡管基類的成員被隱藏,仍然可以訪問基類的成員,通過,base關鍵字,調用基類的引用. 例子: abstract public class Contact { private string address; private string city; private string state; private string zip;
public string FullAddress() { string fullAddress = address + '\n' + city + ',' + state + ' ' + zip;
return fullAddress; } }
public class SiteOwner : Contact { public string FullAddress() { string fullAddress = base.FullAddress();
// do some other stuff... return fullAddress; } } 在例子中,派生類調用基類的成員,用base引用. visioning(版本) 例子: using System; public class WebSite { public string SiteName; public string URL; public string Description;
public WebSite() { }
public WebSite( string strSiteName, string strURL, string strDescription ) { SiteName= strSiteName; URL = strURL; Description = strDescription; }
public override string ToString() { return SiteName + ", " + URL + ", " + Description; } }
public class Contact { public string address; public string city; public string state; public string zip;
public string FullAddress() { string fullAddress = address + '\n' + city + ',' + state + ' ' + zip;
return fullAddress; } }
public class SiteOwner : Contact { int siteHits; string name; WebSite mySite;
public SiteOwner() { mySite = new WebSite(); siteHits = 0; }
public SiteOwner(string aName, WebSite aSite) { mySite = new WebSite(aSite.SiteName, aSite.URL, aSite.Description);
Name = aName; }
new public string FullAddress() { string fullAddress = mySite.ToString();
return fullAddress; }
public string Name { get { siteHits++; return name; } set { name = value; siteHits = 0; } } }
public class Test { public static void Main() { WebSite mySite = new WebSite("Le Financier", "http://www.LeFinancier.com", "Fancy Financial Site");
SiteOwner anOwner = new SiteOwner("John Doe", mySite); string address;
anOwner.address = "123 Lane Lane"; anOwner.city= "Some Town"; anOwner.state= "HI"; anOwner.zip = "45678";
address = anOwner.FullAddress(); // Different Results Console.WriteLine("Address: \n{0}\n", address);
} } 例子中,派生類用new修飾符,說明,和基類有同樣名字的成員. sealed classed(密封類) 密封類是不能被繼承的類.為了避免從一個類中繼承,就要生成密封類. 例子: //密封類 public sealed class CustomerStats { string gender; decimal income; int numberOfVisits;
public CustomerStats() { } }
public class CustomerInfo : CustomerStats // error { }
public class Customer { CustomerStats myStats; // okay } 例子中,密封類不能被繼承.
|