关于C#面向对象三个特征:继承,封装,多态的说明(2)

发表于:2007-06-30来源:作者:点击数: 标签:
封装(Encapsulation):封装是一个 面向对象 的概念,对外部世界,隐藏类的内部. 封装优点: 1.好的封装能减少耦合. 2.类的内部的实现可以自由改变. 3.一个类有更清楚的接口. Data Hiding(数据隐藏):封装的一个最有用的形式是数据隐藏.一个类的数据表现一个对象的
封装(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",派生类重载了索引器.

原文转自:http://www.ltesting.net