1 / 69

第 3 章 面向对象的编程基础

3.1 面向对象的程序设计 3.1.1 类的组织 3.1.2 构造函数 3.1.3 方法 3.1.4 属性 3.1.5 事件. 3.2 常用类操作和数据处理 3.2.1 Convert 类 3.2.2 String 类 3.2.3 StringBuilder 类 3.2.4 DataTime 类和 TimeSpan 类 3.2.5 Math 类. 第 3 章 面向对象的编程基础. 3.1 面向对象的程序设计. 类与对象  类: 一组具有相同数据结构和相同操作的对象的 集合 . 例如 : 汽车

Télécharger la présentation

第 3 章 面向对象的编程基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3.1 面向对象的程序设计 3.1.1 类的组织 3.1.2 构造函数 3.1.3 方法 3.1.4 属性 3.1.5 事件 3.2 常用类操作和数据处理 3.2.1 Convert类 3.2.2 String类 3.2.3 StringBuilder类 3.2.4 DataTime类和 TimeSpan类 3.2.5 Math类 第3章 面向对象的编程基础

  2. 3.1 面向对象的程序设计 类与对象  类:一组具有相同数据结构和相同操作的对象的 集合. 例如:汽车  对象(实例):类的实例化 . 例如:卡车  要使用对象,必须先定义类,然后再创建对象。

  3. 3.1.1 类的组织 1.类的声明 用class定义类,声明类的形式为: [附加声明] [访问修饰符] class 类名称[:[基类] [,接口序列]] { [字段声明] [构造函数] [方法] [事件] } 注意:[]中的内容可省或任选其一,当两者都有时,先基类后接口

  4. 3.1.1 类的组织(续) public class Child { private int age; private string name; // 不带参数的构造函数 public Child() { name = "none"; } // 带参数的构造函数 public Child(string name, int age) {

  5. 3.1.1 类的组织(续) this.name = name; this.age = age; } // 输出方法 public void PrintChild() { Console.WriteLine("{0}, {1} years old.", name, age); } } public class Program {

  6. 3.1.1 类的组织(续) public static void Main() { //使用new关键字创建对象,new后是调用的构造函数 Child child1 = new Child("Zhang San", 11); Child child2 = new Child("Li Si", 10); Child child3 = new Child(); // 显示结果 Console.Write("Child #1: "); child1.PrintChild(); Console.Write("Child #2: "); child2.PrintChild(); Console.Write("Child #3: "); child3.PrintChild();

  7. 3.1.1 类的组织(续) Console.ReadLine(); } } } 2.对象的生存周期 对象在建立时分配了内存,创建对象实际上作了 两个方面的工作: (1)使用new保留字要求系统分配内存; (2)使用构造函数初始化数据。

  8. 3.1.1 类的组织(续) C#也允许在程序中定义析构函数(定义方法与 C++类似),但是 C#不允许在程序中调用析构函 数,而只能由垃圾回收器调用,原因是如果把销毁 对象的工作全部交给编程者通过调用析构函数完 成,而系统不自动实现销毁的功能,一旦编程者忘 记销毁对象,就会引起内存泄漏问题。所以采用垃 圾回收机制自动销毁不再使用的对象。 因此,我们编写程序时不需要定义析构函数,而 是由系统自动定义,自动调用。

  9. 3.1.1 类的组织(续) 注意,虽然C#不允许在程序中直接调用析构函数 销毁对象,但是可以调用安全的Dispose方法销毁 对象,在对性能要求比较高的场合,调用Dispose 方法直接销毁对象还是很有用的。 垃圾回收机制是在它认为适当的时候自动回收不 再使用的内存的,即检测没有被引用的对象,然后 销毁之。 销毁对象也是做了两个方面的工作: (1)释放占用的内存; (2)将分配给对象的内存归还给堆(Heap)。

  10. 3.1.1 类的组织(续) 3 字段和局部变量 字段:指声明为类一级的对象或值类型的变量。 局部变量:指在方法、事件以及构造函数内声明的变量。 public class Program { public static int j=20; //字段 public static void Main() {

  11. 3.1.1 类的组织(续) int j=30; //局部变量 Console.WriteLine(j); //输出结果:30 Console.WriteLine(Program.j); //输出结果:20 Console.ReadLine(); } } 当字段和局部变量名相同时,如果要引用静态字段,可以 使用下面的形式: 类名.字段名 如果是实例字段,则使用下面的形式: this.字段名 这里的this指当前实例。 当然,如果没有出现字段和局部变量名重名的情况,引用 字段的形式和引用局部变量的形式相同。

  12. 3.1.1 类的组织(续) 4. 静态成员与实例成员 类的成员 类的成员包括字段、属性、构造函数、方法、事件、索引、嵌套类。 类的成员分静态成员和实例成员 静态成员在内存中只有一份 静态成员要等到应用程序结束时才会退出内存。 把只有创建了类的实例才能够使用的成员叫实例成员。

  13. 3.1.1 类的组织(续) 5. 访问修饰符 C#中有以下成员访问修饰符: Public (常用)任何外部的类都可以不受限制的存 取这个类的方法和数据成员。 private (常用)类中的所有方法和数据成员只能在 此类中使用,外部无法存取。(默认) Protected除了让本身的类可以使用之外,任何继承 自此类的子类都可以存取。

  14. 3.1.1 类的组织(续) Internal在当前项目中都可以存取。该访问权限一 般用于基于组件的开发,因为它可以使组件以私有 方式工作,而该项目外的其它代码无法访问。 Protected internal 只限于当前项目,或者从该项目 的类继承的类才可以存取。 Partial 局部类型,类的定义和实现可以分布在多个 文件中,但都要使用partial标注,基类只需要声明 一次,若多次声明则必须完全一致。

  15. 3.1.2 构造函数 构造函数是一个特殊的方法,用于在建立对象时 进行初始化的动作,在C#中,每当创建一个对象 时,都会先调用类中定义的构造函数。 使用构造函数的好处是它能够确保每一个对象在 被使用之前都适当地进行了初始化的动作。 另外,构造函数还具有以下特点: 1) 每个类至少有一个构造函数。若程序代码中没 有构造函数则系统会自动提供一个默认的构造函数。2) 一个构造函数总是和它的类名相同。

  16. 3.1.2 构造函数(续) 3) 构造函数不包含任何返回值。 • 默认构造函数 (1)如果在类中不定义构造函数,系统会提供一个 默认的构造函数。 (2)默认构造函数没有参数。

  17. 3.1.2 构造函数(续) (3)默认构造函数自动将非静态成员初始化为: 数值型:如int、double等,初始化为0。 bool类型:初始化为false. 引用类型:初始化为null。 (4)如果自己定义了构造函数,则不会自动进行初 始化。

  18. 3.1.2 构造函数(续) 2. 重载构造函数 有时候可能会遇到这样的情况:在一个类中的多 个方法中都要用到某一个数据成员,而该成员值必 须从其他类中传递过来。这时,无参数的构造函数 就不能胜任了,解决这个问题最好的办法就是:重 载(Overloading)构造函数。

  19. 3.1.2 构造函数(续) using System; using System.Collections.Generic; using System.Text; namespace OverloadingExample { class Program { public Program() { Console.WriteLine("null"); }

  20. 3.1.2 构造函数(续) public Program(string str) { Console.WriteLine(str); } static void Main() { Program aa = new Program(); Program bb = new Program("How are you!"); Console.ReadLine(); } } }

  21. 3.1.3 方法 方法(Method)是一组程序代码的集合,每个方 法都有一个方法名,便于识别和让其他方法调用。 1. 方法的定义与使用 (1)方法必须放在某个类中。 (2)定义方法的语法形式为: 访问修饰符 返回值类型 方法名称(参数序列) { 语句序列 }

  22. 3.1.3 方法(续) 定义方法时,需要注意以下几点: 方法名不能和变量、常数或者任何声明在类中其 它的成员相同。 方法可以有参数,也可以没有参数,但是不论是否有参数,小括号都是必需的。如果参数序列中的参数有多个,则以逗号分开。 结束某个方法的执行,可以使用return语句,程序遇到return语句后,会将执行流程交还给调用此方法的程序代码段。此外,还可以用return语句返回一个值。

  23. 3.1.3 方法(续) • 如果声明一个非void类型的方法,则方法中必须至少有一个return语句。 using System; using System.Collections.Generic; using System.Text; namespace MethodExample { class Program { public int MyMethod() {

  24. 3.1.3 方法(续) Console.WriteLine("this is MyMethod."); int i = 10; return i; } static void Main() { Program method = new Program(); int j = 5; j = method.MyMethod();

  25. 3.1.3 方法(续) Console.WriteLine("the value is {0}.", j); Console.ReadLine(); } } } 2. 方法中的参数传递 1) 传递值类型的参数 值类型参数的格式为: 参数类型 参数名

  26. 3.1.3 方法(续) using System; using System.Collections.Generic; using System.Text; namespace ValueTransferExample { class Program { public static void AddOne(int a) { a++; }

  27. 3.1.3 方法(续) static void Main() { int a = 3; Console.WriteLine("调用AddOne之前,a={0}", a); AddOne(a); Console.WriteLine("调用AddOne之后,a={0}", a); Console.ReadLine(); } } }

  28. 3.1.3 方法(续) 2) 传递引用类型的参数 引用类型参数的格式为: ref 参数类型 参数名 using System; using System.Collections.Generic; using System.Text; namespace ReferenceTransferExample { class Program { public static void AddOne(ref int a) {

  29. 3.1.3 方法(续) a++; } static void Main() { int x = 3; Console.WriteLine("调用AddOne之前,x={0}", x); AddOne(ref x); Console.WriteLine("调用AddOne之后,x={0}", x); Console.ReadLine(); } } }

  30. 3.1.3 方法(续) 3) 输出多个引用类型的参数 输出引用类型参数的格式为: out 参数类型 参数名 using System; using System.Collections.Generic; using System.Text; namespace ReferenceOutExample { class Program { public static void MyMethod(out int a, out int b)

  31. 3.1.3 方法(续) { a = 5; b = 6; } static void Main() { int x, y; MyMethod(out x, out y); Console.WriteLine("调用MyMethod之后,x={0},y={1}", x, y); Console.ReadLine(); } } }

  32. 3.1.3 方法(续) 4) 传递个数不确定的参数 需要传递的参数个数不确定时,可以采用params 关键字。 using System; using System.Collections.Generic; using System.Text; namespace UncertaintyTransferExample { class Program { public static float Average(params long[] v) { long total, i;

  33. 3.1.3 方法(续) for (i = 0, total = 0; i < v.Length; ++i) total += v[i]; return (float)total / v.Length; } static void Main() { float x = Average(1, 2, 3, 5); Console.WriteLine("1、2、3、5的平均值为{0}", x); x = Average(4, 5, 6, 7, 8); Console.WriteLine("4、5、6、7、8的平均值为{0}", x); Console.ReadLine(); } } }

  34. 3.1.3 方法(续) 3. 方法重载 方法重载是指具有相同的方法名,但参数类型或 参数个数不完全相同的多个方法可以同时出现在一 个类中。 using System; using System.Collections.Generic; using System.Text; namespace MethodOverloadingExample { class Program {

  35. 3.1.3 方法(续) public static int Add(int i, int j) { return i + j; } public static string Add(string s1, string s2) { return s1 + s2; } public static long Add(long x) { return x + 5;

  36. 3.1.3 方法(续) } static void Main() { Console.WriteLine(Add(1, 2)); Console.WriteLine(Add("1", "2")); Console.WriteLine(Add(10)); //按回车键结束 Console.ReadLine(); } } }

  37. 3.1.4 属性 作用:可以限制外部对类中成员变量的存取权限。 1.属性的定义 get:用来读取数据成员的值。 set:用来设置数据成员的值。 using System; using System.Collections.Generic; using System.Text; namespace PropertyExample { public class MyClass

  38. 3.1.4 属性(续) { private int number = 0; public int MyNumber { get { return number; } set { if (value > 0)

  39. 3.1.4 属性(续) { number = value; //value是关键字,其值由编译器自动生成。 } } } } public class Program { public static void Main() { MyClass me = new MyClass();

  40. 3.1.4 属性(续) Console.WriteLine(me.MyNumber); me.MyNumber = 5; Console.WriteLine(me.MyNumber); Console.ReadLine(); } } }

  41. 3.1.4 属性(续) 2. 属性与方法的区别 属性和方法的区别主要有: 1) 属性不必使用括号,但方法一定要使用括号。 2) 属性不能指定参数,方法可以指定参数。 3) 属性不能使用void类型,方法则可以使用void类型。

  42. 3.1.5 事件 事件:是指当对象发生某些事情时,向其他对象提 供通知的一种方法。 两种角色: 1) 一个是事件发送方 2) 一个是事件接收方 用途:事件最常见的用途是用于图形用户界面 。

  43. 3.2 常用类操作和数据处理 Visual Studio 2005开发环境提供了实现各种功能 的丰富的类,其中有些类是实际编程中经常用到的, 这一节只选取了其中的几种,并通过一些具体例子简 单说明了使用方法,希望能起到抛砖引玉的作用。

  44. 3.2.1 Convert类 Convert类位于System命名空间下,用于将一个值 类型转换为另一个值类型。 using System; using System.Collections.Generic; using System.Text; namespace ConvertClassExample { class Program { public static void Main()

  45. 3.2.1 Convert类(续) { double d1 = 23.5D, d2 = 23.4D; int i1 = Convert.ToInt32(d1); int i2 = Convert.ToInt32(d2); Console.WriteLine("{0},{1}", i1, i2); int i = 0; bool b1 = Convert.ToBoolean(d1); bool b2 = Convert.ToBoolean(i); Console.WriteLine("{0},{1}", b1, b2); string s = "123";

  46. 3.2.1 Convert类(续) i = Convert.ToInt32(s); Console.WriteLine("{0},{1}", s, i); Console.ReadLine(); } } } 输出结果: 24,23 True,False 123,123

  47. 3.2.2 String类(续) 字符串类分为两种: String类和StringBuilder类 • String类的表示方法: string str = "C:\\test\\first.cs" string str = @"C:\test\first.cs" • 取字符串中的某个字符的方法: string myString = "some text"; char chFirst = myString[2]; //结果为m

  48. 3.2.2 String类(续) 注意: string myString = "some text"; myString += " and a bit more"; 其实际操作并不是在原来myString所占内存空间的 后面直接附加上第二个字串,而是返回一个新String 实例,即重新为新字符串分配内存空间。

  49. 3.2.2 String类(续) 1.字符串的表示格式 可以使用Format方法将字符串表示为规定格式。 规定格式的一般形式为: {N [, M][: 格式码]} 其中: [ ]表示其中的内容为可选项。 N:从零开始的整数,表示第几个参数。 M:可选整数,表示最小宽度。若该参数的长度小于M,就用空格填充。如果M为负,则左对齐;如果M为正,则右对齐。如果未指定M,则默认为零。

  50. 3.2.2 String类(续) 格式码是可选的格式化代码字符串。(详细内容 请搜索“格式化字符串”查看) 必须用“{”和“}”将格式与其他字符分开。如果恰 好在格式中也要使用大括号,可以用连续的两个大 括号表示一个大括号,即: “{{”或者“}}”。

More Related