下面重点说明面向对象的三个特征.继承,封装和多态.
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
}
例子中,密封类不能被继承.
……