当前位置:编程学习 > C#/ASP.NET >>

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

答案:封装(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 adde

上一个:关于C#面向对象三个特征:继承,封装,多态的说明(1)
下一个:类的继承与封装应用

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,