C程序的相关设计基础.ppt

  1. 1、本文档共65页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
关键字static 案例名称:使用static关键字 程序名称:2-28.cs ? using System; public class Person { private int id; public static int total = 0; public Person() { total++; id = total; } } public class OtherClass { public static void Main() { Person.total = 100; Console.WriteLine (Person.total); Person c = new Person(); Console.WriteLine (Person.total); } } 案例名称:使用静态方法 程序名称:2-29.cs ? using System; public class Person { private int id; private static int total = 0; public static int getTotalPerson() { return total; } public Person() { total++; id = total; } } public class TestPerson { public static void Main() { Console.WriteLine (Person.getTotalPerson()); Person p1 = new Person(); Console.WriteLine (Person.getTotalPerson()); } } C#面向对象高级特性 和其他的面向对象语言一样,C#支持多态、虚方法、函数的重载等。 除此之外,C#还提供一种特殊的数据形态“装箱” 多态(Polymorphism) 在C#中,多态性的定义是:“同一操作作用于不同的类的实例,不同的类将进行不同的解释,最后产生不同的执行结果”。C#支持两种类型的多态性。 编译时的多态性:编译时的多态是通过重载来实现的。对于非虚的成员来说,系统在编译时,根据传递的参数、返回的类型等信息决定实现何种操作。 运行时的多态性:运行时的多态性是直到系统运行时,才根据实际情况决定实现何种操作。C#中,运行时的多态性通过虚方法实现。 编译时的多态性提供了运行速度快的特点,而运行时的多态性则带来了高度灵活和抽象的特点。 虚方法 案例名称:使用虚方法 程序名称:2-30.cs ? using System; class Test{ static void Main(string[] args) { Base b = new Base(); b.Draw(); Derived d = new Derived(); d.Draw(); d.Fill(); Base obj = new Derived(); obj.Fill(); obj.Draw(); } } class Base{ public void Fill() { System.Console.WriteLine("Base.Fill"); } public virtual void Draw() { System.Console.WriteLine("Base.Draw"); } } class Derived : Base{ public override void Draw() { System.Console.WriteLine("Derived.Draw"); } public new void Fill() { System.Console.WriteLine("Derived.Fill"); } } 抽象类 案例名称:使用抽象类 程序名称:2-31.cs ? using System; abstract public class Window{ public Window(int top, int left) { this.top = top; this.left = left; } abstract public void DrawWindow( ); protected int top; protected int left; } public class ListBox : Window{ public ListBox( int top, int left

文档评论(0)

beautyeve + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档