明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

對(duì)于C#面向?qū)ο?個(gè)特征:繼承,封裝,多態(tài)的說明(1)

[摘要]下面重點(diǎn)說明面向?qū)ο蟮娜齻(gè)特征.繼承,封裝和多態(tài).C#是一種現(xiàn)代的面向?qū)ο蟮恼Z言.繼承(inheritance):繼承是一個(gè)面向?qū)ο蟮脑~語.說明,一個(gè)類(派生類)能分享,其它類(基類)的特征和行為....
下面重點(diǎn)說明面向?qū)ο蟮娜齻(gè)特征.繼承,封裝和多態(tài).
C#是一種現(xiàn)代的面向?qū)ο蟮恼Z言.
繼承(inheritance):繼承是一個(gè)面向?qū)ο蟮脑~語.說明,一個(gè)類(派生類)能分享,其它類(基類)的特征和行為.派

生類和基類是"is a"的關(guān)系.
base classes(基類):通;惪梢宰约簩(shí)例化,或被繼承.派生類繼承基類中的成員,被標(biāo)記為protected或更大

的權(quán)限.語法: class (derive class name):(base class name)
例子:
//基類
public class Contact
{
//默認(rèn)私有的字段
string name;
string email;
string address;
//構(gòu)造函數(shù)
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(抽象類):抽象類是一種特殊的基類.除過普通的類成員,它們還有抽象的類成員.抽象類成員,

是不能被實(shí)例化的方法和屬性.所有直接從抽象類派生的類,必須實(shí)現(xiàn)抽象的方法和屬性.抽象類不能被實(shí)例化.
例子:
//抽象類
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;
}
}
}
上面的例子,定義了三個(gè)類.一個(gè)抽象類,兩個(gè)派生類.實(shí)現(xiàn)了父類的方法和屬性."override"修飾符,實(shí)現(xiàn)了抽象

類的方法.
Calling Base Class Members(調(diào)用基類成員)
派生類能調(diào)用基類成員,如果,成員的修飾符是"protected"或更大權(quán)限.在適當(dāng)?shù)纳舷挛臈l件下,好像調(diào)用自己的

成員一樣.
例子:
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;
}
}
上面的例子中,派生類調(diào)用基類的方法:FullAddress();
基類的構(gòu)造函數(shù),可以被派生類調(diào)用,用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成員,可以調(diào)用基類的構(gòu)造函數(shù).
Hiding Base Class Members(隱藏基類成員)
派生類可以和基類有同樣名字的成員.這時(shí),就會(huì)隱藏基類的成員.
例子:
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(),當(dāng)調(diào)用時(shí),基類的方法會(huì)被隱藏.

盡管基類的成員被隱藏,仍然可以訪問基類的成員,通過,base關(guān)鍵字,調(diào)用基類的引用.
例子:
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;
}
}
在例子中,派生類調(diào)用基類的成員,用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(密封類)
密封類是不能被繼承的類.為了避免從一個(gè)類中繼承,就要生成密封類.
例子:
//密封類
public sealed class CustomerStats
{
string gender;
decimal income;
int numberOfVisits;

public CustomerStats()
{
}
}

public class CustomerInfo : CustomerStats // error
{
}

public class Customer
{
CustomerStats myStats; // okay
}
例子中,密封類不能被繼承.