封装(encapsulation):封装是一个面向对象的概念,对外部世界,隐藏类的内部.
封装优点:
1.好的封装能减少耦合.
2.类的内部的实现可以自由改变.
3.一个类有更清楚的接口.
data hiding(数据隐藏):封装的一个最有用的形式是数据隐藏.一个类的数据表现一个对象的状态.
修饰符支持封装:
private:只有类本身能存取.
protected:类和派生类可以存取.
internal:只有同一个项目中的类可以存取.
protected internal:是protected和internal的结合.
public:完全存取.
other encapsulating strategy:(其他封装策略)属性和索引器的目的是封装一个类的细节和给类的用户提供一个公共的接口.
封装和继承的关系:
封装的意思是包容(聚合),类与类之间的关系是"has a".一个类里面有另一个类.
继承,类与类之间的关系是"is a".
多态(polymorphism):就是怎样重载一个虚拟类.多态是面向对象的重要概念.
implementing polymorphism(实现多态):
例子:
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;
}
}
abstract public class contact
{
public virtual string updatenotify()
{
return "web site change notification";
}
}
public class customer : contact
{
public new string updatenotify()
{
return @"
this is to let you know your
favorite site, financial times,
has been updated with new links";
}
}
public class siteowner : contact
{
website mysite;
public siteowner(string aname, website asite)
{
mysite = new website(asite.sitename,
asite.url,
asite.description);
}
public new string updatenotify()
{
return @"
this is to let you know your site, " + "\n" +
mysite.sitename + @", has been added as
a link to financial times.";
}
}
public class test
{
public static void main()
{
website lefin = new website("le financier",
"http://www.lefinancier.com",
"fancy financial site");
contact[] contacts = new contact[2];
contacts[0] = new siteowner("pierre doe", lefin);
contacts[1] = new customer();
foreach (contact poc in contacts)
{
if (poc is siteowner)
{
console.writeline("message: {0}\n",
((siteowner)poc).updatenotify());
}
else
{
console.writeline("message: {0}\n",
((customer)poc).updatenotify());
}
}
}
}
在例子中,contact类有个虚拟方法,有两个派生类分别实现.使用了"new"关键字.
可以有更有效和优雅的方法,实现它,就是多态.
例子:
using system;
abstract public class contact
{
public virtual string updatenotify()
{
return "web site change notification";
}
}
public class customer : contact
{
public override string updatenotify()
{
return @"
this is to let you know your
favorite site, financial times,
has been updated with new links";
}
}
public class siteowner : contact
{
string sitename;
public siteowner(string sname)
{
sitename = sname;
}
public override string updatenotify()
{
return @"
this is to let you know your site, " + "\n" +
sitename + @", has been added as
a link to financial times.";
}
}
public class test
{
public static void main()
{
contact[] contacts = new contact[2];
contacts[0] = new siteowner("le financier");
contacts[1] = new customer();
foreach (contact poc in contacts)
{
console.writeline("message: {0}\n",
poc.updatenotify());
}
}
}
例子中,派生类用"override"实现了多态.
虚拟方法是允许多态工作的基类的方法.用"override"修饰符说明,能被派生类重载.虚拟方法和抽象方法的不同
时,虚拟方法有实现,抽象方法没有.抽象方法,隐式说明是虚拟,必须被重载;虚拟方法不必被重载.
多态,必须是虚拟方法,而且,方法的签名必须一致,包括方法名称,参数,和参数类型.
例子:
abstract public class contact
{
public virtual string updatenotify()
{
return "web site change notification";
}
}
public class customer : contact
{
public override string sendmail() {}// error
public override string updatenotify(int number) {}// error
}
例子中,sendmail不是虚拟方法,故错误;updatenotify,带有不同的参数,故也错误.
new 和 override 修饰符,都可以实现新的方法.但,new 实现的是派生类新的方法.
例子:
using system;
abstract public class contact
{
public virtual string updatenotify()
{
return "web site change notification";
}
}
public class customer : contact
{
public new string updatenotify()
{
return @"
this is to let you know your
favorite site, financial times,
has been updated with new links";
}
}
public class siteowner : contact
{
string sitename;
public siteowner(string sname)
{
sitename = sname;
}
public override string updatenotify()
{
return @"
this is to let you know your site, " + "\n" +
sitename + @", has been added as
a link to financial times.";
}
}
public class test
{
public static void main()
{
contact[] contacts = new contact[2];
contacts[0] = new siteowner("le financier");
contacts[1] = new customer();
foreach (contact poc in contacts)
{
console.writeline("message: {0}\n",
poc.updatenotify());
}
}
}
结果是:
message:
this is to let you know your site,
le financier, has been added as
a link to financial times.
message: web site change notification
例子中,customer 用"new"实现新的方法,但是,在运行是不是多态.仍然调用基类的方法.
most-derived implementations(多重派生实现)
polymorphic properties(多态的属性):c#允许,属性的多态实现.
例子:
using system;
public class sitestats
{
public int numberofvisits = 0;
}
abstract public class contact
{
protected string name;
public virtual string name
{
get
{
return name;
}
set
{
name = value;
}
}
}
public class customer : contact
{
sitestats mystats = new sitestats();
public override string name
{
get
{
mystats.numberofvisits++;
console.writeline("number of visits: {0}",
mystats.numberofvisits);
return name;
}
set
{
base.name = value;
mystats.numberofvisits = 0;
console.writeline("name: {0}", name);
}
}
}
public class test
{
public static void main()
{
contact mycontact = new customer();
mycontact.name = "george";
}
}
例子中,抽象类,有属性name,派生类重载实现了属性.
polymorphic indexers(多态的索引器):索引器的多态.
例子:
using system;
using system.collections;
public class sitelist
{
protected sortedlist sites;
public sitelist()
{
sites = new sortedlist();
}
public int nextindex
{
get {
return sites.count;
}
}
public virtual string this[int index]
{
get
{
return (string) sites.getbyindex(index);
}
set
{
sites[index] = value;
}
}
}
public class financialsitelist : sitelist
{
public override string this[int index]
{
get
{
console.writeline("financialsitelist indexer get");
if (index > sites.count)
return (string)null;
return base[index];
}
set
{
console.writeline("financialsitelist indexer set");
base[index] = value;
}
}
}
class sitemanager
{
sitelist sites = new sitelist();
public static void main()
{
sitemanager mgr = new sitemanager();
mgr.sites = new financialsitelist();
mgr.sites[mgr.sites.nextindex] = "great site!";
console.writeline("site: {0}",
mgr.sites[0].tostring());
}
}
例子中,基类的索引器是"virtual",派生类重载了索引器.
posted on 2007-04-04 01:02
曾科 阅读(808)
评论(1) 编辑 收藏 所属分类:
C#