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

72
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

description

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.1 面向对象的程序设计. 类与对象  类: 一组具有相同数据结构和相同操作的对象的 集合 . 例如 : 汽车 - PowerPoint PPT Presentation

Transcript of 第三章 面向对象的编程基础

Page 1: 第三章   面向对象的编程基础

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类

Page 2: 第三章   面向对象的编程基础

2

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

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

33.1.1 类的组织1. 类的声明

用 class 定义类,声明类的形式为:[ 附加声明 ] [ 访问修饰符 ] class 类名称 [ : [ 基类 ] [ ,接口序列 ]]{

[ 字段声明 ] [ 构造函数 ] [ 方法 ] [ 事件 ]

}

注意 :[] 中的内容可省或任选其一 , 当两者都有时 , 先基类后接口

Page 4: 第三章   面向对象的编程基础

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

Page 5: 第三章   面向对象的编程基础

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

Page 6: 第三章   面向对象的编程基础

63.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();

Page 7: 第三章   面向对象的编程基础

73.1.1 类的组织(续) Console.ReadLine(); } }}

Page 8: 第三章   面向对象的编程基础

8

2. 对象的生存周期 对象在建立时分配了内存,创建对象实际上作了两个方面的工作:

( 1 )使用 new 保留字要求系统分配内存;( 2 )使用构造函数初始化数据。

Page 9: 第三章   面向对象的编程基础

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

Page 10: 第三章   面向对象的编程基础

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

( 1 )释放占用的内存; ( 2 )将分配给对象的内存归还给堆( Heap )。

Page 11: 第三章   面向对象的编程基础

113.1.1 类的组织(续)3 字段和局部变量

字段 : 指声明为类一级的对象或值类型的变量。 局部变量 : 指在方法、事件以及构造函数内声明的变量。

public class Program { public static int j=20; // 字段 public static void Main() {

Page 12: 第三章   面向对象的编程基础

123.1.1 类的组织(续) int j=30; // 局部变量 Console.WriteLine(j); // 输出结果: 30 Console.WriteLine(Program.j); // 输出结果: 20 Console.ReadLine(); } }

Page 13: 第三章   面向对象的编程基础

13

当字段和局部变量名相同时,如果要引用静态字段,可以使用下面的形式: 类名 . 字段名 如果是实例字段,则使用下面的形式: this. 字段名 这里的 this 指当前实例。

当然,如果没有出现字段和局部变量名重名的情况,引用字段的形式和引用局部变量的形式相同。

Page 14: 第三章   面向对象的编程基础

143.1.1 类的组织(续)4. 静态成员与实例成员类的成员 类的成员包括字段、属性、构造函数、方法、事件、索引、嵌套类。

类的成员分静态成员和实例成员静态成员在内存中只有一份静态成员要等到应用程序结束时才会退出内存。 把只有创建了类的实例才能够使用的成员叫实例成员。

Page 15: 第三章   面向对象的编程基础

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

Page 16: 第三章   面向对象的编程基础

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

Page 17: 第三章   面向对象的编程基础

173.1.2 构造函数 构造函数是一个特殊的方法,用于在建立对象时进行初始化的动作,在 C# 中,每当创建一个对象时,都会先调用类中定义的构造函数。 使用构造函数的好处是它能够确保每一个对象在被使用之前都适当地进行了初始化的动作。

Page 18: 第三章   面向对象的编程基础

18

另外,构造函数还具有以下特点: 1) 每个类至少有一个构造函数。若程序代码中没有构造函数则系统会自动提供一个默认的构造函数。 2) 一个构造函数总是和它的类名相同。 3) 构造函数不包含任何返回值。

Page 19: 第三章   面向对象的编程基础

193.1.2 构造函数(续)

1. 默认构造函数( 1 )如果在类中不定义构造函数,系统会提供一个默认的构造函数。( 2 )默认构造函数没有参数。

Page 20: 第三章   面向对象的编程基础

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

Page 21: 第三章   面向对象的编程基础

213.1.2 构造函数(续)2. 重载构造函数

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

Page 22: 第三章   面向对象的编程基础

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

Page 23: 第三章   面向对象的编程基础

233.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(); } }}

Page 24: 第三章   面向对象的编程基础

243.1.3 方法 方法( Method )是一组程序代码的集合,每个方法都有一个方法名,便于识别和让其他方法调用。

1. 方法的定义与使用 ( 1 )方法必须放在某个类中。 ( 2 )定义方法的语法形式为: 访问修饰符 返回值类型 方法名称(参数序列)

{ 语句序列 }

Page 25: 第三章   面向对象的编程基础

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

Page 26: 第三章   面向对象的编程基础

263.1.3 方法(续) 如果声明一个非 void 类型的方法,则方法中必须至少有一个 return语句。

using System;using System.Collections.Generic;using System.Text;namespace MethodExample{ class Program { public int MyMethod() {

Page 27: 第三章   面向对象的编程基础

273.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();

Page 28: 第三章   面向对象的编程基础

283.1.3 方法(续) Console.WriteLine("the value is {0}.", j); Console.ReadLine(); } }}

Page 29: 第三章   面向对象的编程基础

293.1.3 方法(续)2. 方法中的参数传递

1) 传递值类型的参数 值类型参数的格式为: 参数类型 参数名using System;using System.Collections.Generic;using System.Text;namespace ValueTransferExample{ class Program { public static void AddOne(int a) { a++; }

Page 30: 第三章   面向对象的编程基础

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

Page 31: 第三章   面向对象的编程基础

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

Page 32: 第三章   面向对象的编程基础

323.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(); } }}

Page 33: 第三章   面向对象的编程基础

333.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)

Page 34: 第三章   面向对象的编程基础

343.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(); } }}

Page 35: 第三章   面向对象的编程基础

353.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;

Page 36: 第三章   面向对象的编程基础

363.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(); } }}

Page 37: 第三章   面向对象的编程基础

373.1.3 方法(续)3. 方法重载 方法重载是指具有相同的方法名,但参数类型或参数个数不完全相同的多个方法可以同时出现在一个类中。

using System;using System.Collections.Generic;using System.Text;namespace MethodOverloadingExample{ class Program {

Page 38: 第三章   面向对象的编程基础

383.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;

Page 39: 第三章   面向对象的编程基础

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

Page 40: 第三章   面向对象的编程基础

403.1.4 属性作用:可以限制外部对类中成员变量的存取权限。1. 属性的定义 get :用来读取数据成员的值。 set :用来设置数据成员的值。

using System;using System.Collections.Generic;using System.Text;namespace PropertyExample{ public class MyClass

Page 41: 第三章   面向对象的编程基础

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

Page 42: 第三章   面向对象的编程基础

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

Page 43: 第三章   面向对象的编程基础

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

Page 44: 第三章   面向对象的编程基础

443.1.4 属性(续)2. 属性与方法的区别

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

Page 45: 第三章   面向对象的编程基础

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

Page 46: 第三章   面向对象的编程基础

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

Page 47: 第三章   面向对象的编程基础

473.2.1 Convert 类 Convert 类位于 System命名空间下,用于将一个值类型转换为另一个值类型。

using System;using System.Collections.Generic;using System.Text;namespace ConvertClassExample{ class Program { public static void Main()

Page 48: 第三章   面向对象的编程基础

483.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";

Page 49: 第三章   面向对象的编程基础

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

Page 50: 第三章   面向对象的编程基础

503.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

Page 51: 第三章   面向对象的编程基础

513.2.2 String 类(续)注意 :

string myString = "some text"; myString += " and a bit more";

其实际操作并不是在原来myString 所占内存空间的后面直接附加上第二个字串 , 而是返回一个新String 实例,即重新为新字符串分配内存空间。

Page 52: 第三章   面向对象的编程基础

523.2.2 String 类(续)1. 字符串的表示格式 可以使用 Format 方法将字符串表示为规定格式。规定格式的一般形式为:

{N [, M][: 格式码 ]} 其中: [ ]表示其中的内容为可选项。 N :从零开始的整数,表示第几个参数。 M :可选整数,表示最小宽度。若该参数的长度小于

M ,就用空格填充。如果 M 为负,则左对齐;如果 M为正,则右对齐。如果未指定 M ,则默认为零。

Page 53: 第三章   面向对象的编程基础

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

Page 54: 第三章   面向对象的编程基础

543.2.2 String 类(续)常用格式举例:

1) 在 ToString 中使用 d格式。int i = 12345;// i.ToString("d")表示将 i按照实际整数宽度转换为字符串string str = i.ToString("d");Console.WriteLine(str); // 结果 12345// i.ToString("d8")表示将 i转换为字符串,宽度为 8位,不够 8位左边用 0填充string str1 = i.ToString("d8");Console.WriteLine(str1); // 结果 00012345

Page 55: 第三章   面向对象的编程基础

553.2.2 String 类(续)2) 在 string.Format 中使用 d格式。

int i = 123;double j = 123.45;// {0,7:d}表示第 0 个参数,宽度 7位,不够 7位左边用空格填充, d表示十进制整数string s1 = string.Format("the value is {0,7:d}",i);Console.WriteLine(s1); // 结果 the value is 123// {0,7:f3}表示第 0 个参数 ,总宽度 7位 , 其中小数部分占 3位 , 不够 7位左边填空格// f表示浮点数string s2 = string.Format("the value is {0,7:f3}",j); Console.WriteLine(s2); // 结果 the value is 123.450

Page 56: 第三章   面向对象的编程基础

563.2.2 String 类(续)3) 在 ToString 中使用 f格式。

double i = 12345.6789;// f2表示浮点数小数部分为 2位,多余部分四舍五入,不够 2位时右边补零string str = i.ToString("f2"); Console.WriteLine(str); // 结果 12345.68string str1 = i.ToString("f6"); Console.WriteLine(str1); // 结果 12345.678900

Page 57: 第三章   面向对象的编程基础

573.2.2 String 类(续)4) 在 ToString 中使用 n格式。

double i = 12345.6789;// n表示整数部分从小数点向左每 3位用逗号分隔开,小数部分 2位,多余部分四舍五入string str = i.ToString("n"); Console.WriteLine(str); // 结果 12,345.68// n4表示整数部分从小数点向左每 3位用逗号分隔开,小数部分 4位,多余部分四舍五入string str1 = i.ToString("n4"); Console.WriteLine(str1); // 结果 12,345.6789

Page 58: 第三章   面向对象的编程基础

583.2.2 String 类(续)5) 在 ToString 中使用 p格式。

double i = 0.126;// p表示百分数形式string s = string.Format("the value is {0:p}",i);Console.WriteLine(s); // 结果 the value is 12.60%string str = i.ToString("p");Console.WriteLine(str); // 结果 12.60%

Page 59: 第三章   面向对象的编程基础

593.2.2 String 类(续)6) 日期与时间的格式化表示。

DateTime dt = new DateTime(2006,5,25);// yy表示年占两位, M表示月占 1位,超出 1位按实际位数// d表示日占 1位,超出 1位按实际位数string str = dt.ToString("yy.M.d");Console.WriteLine(str); // 结果 06.5.25//yyyy表示年占 4位, M表示月占 1位,超出 1位按实际位数,其他按原字符输出string str1 = dt.ToString("yyyy年M月 ");Console.WriteLine(str1); // 结果 2006年 5月

Page 60: 第三章   面向对象的编程基础

603.2.2 String 类(续)7) 指定位数和对齐方式。

int i = 123;double j = 123.45; // {0,-7}表示第 0 个参数左对齐,占 7位,不够 7位右边补空格。// {1,7}表示第 1 个参数右对齐,占 7位,不够 7位左边补空格。string s = string.Format("i:{0,-7}, j:{1,7}", i, j);Console.WriteLine(s); // 结果 i:123 ,j: 123.45

Page 61: 第三章   面向对象的编程基础

613.2.2 String 类(续)8) 使用占位符表示。

int i = 00012;string str = string.Format("{0:###,###.00}",i);Console.WriteLine(str); // 结果 12.00i = 0;str = string.Format("{0:###}", i);Console.WriteLine(str); // 结果什么都不输出

Page 62: 第三章   面向对象的编程基础

623.2.2 String 类(续)2. 常用字符串操作方法 举例说明常用的一些方法。生成字符串

string str1 = "this is a string.";string str2 = str1;string str3 = new string('a',4); Console.WriteLine(str3); // 结果为 aaaa

查找指定字符串在字符串中出现的位置IndexOf(string str) LastIndexOf(string str)

Page 63: 第三章   面向对象的编程基础

633.2.2 String 类(续)例如 :

Console.WriteLine(str1.IndexOf("is")); // 结果为 2( 注意序号从 0开始 )

Console.WriteLine(str1.LastIndexOf("is")); // 结果为 5删除指定数量的字符

Remove(int startIndex,int count) 例如 :

string str1 = "this is a string.“Console.WriteLine(str1.Remove(1,2)); // 结果为 ts is a string

Page 64: 第三章   面向对象的编程基础

643.2.2 String 类(续)字符串替换

Replace (string oldStr,string newStr) 例如 :

string str1 = "this is a string.“Console.WriteLine(str1.Replace("is","xy")); // 结果为 thxy xy a string

分离字符串 Split(char[] separator)

例如 :string str1 = "this is a string.“

Page 65: 第三章   面向对象的编程基础

653.2.2 String 类(续)string[ ] str = str1.Split(‘.'); for(int i = 0;i < str.Length;i++) { Console.WriteLine(str[i]); }

输出结果 : this is a string

Page 66: 第三章   面向对象的编程基础

663.2.2 String 类(续)把字符串中的字符复制到一个字符数组中 利用 ToCharArray 可以将字符串转换为字符数组。

字符串中字母的大小写转换 利用 ToUpper 可以将字符串的所有英文字母转换为大写,利用 ToLower 可以将字符串的所有英文字母转换为小写。从字符串开头或结尾删除指定的字符 利用 TrimStart删除字符串首部空格,利用 TrimEnd删除字符串尾部空格,利用 Trim删除字符串首部和尾部空格。

Page 67: 第三章   面向对象的编程基础

673.2.2 String 类(续)例如 :

string s1 = "□□□□this is a book";string s2 = "that is a pen□□□□";string s3 = "□□is a pen□□ ";Console.WriteLine(s1.TrimStart()); //删除首部空格Console.WriteLine(s2.TrimEnd()); //删除尾部空格Console.WriteLine(s3.Trim()); //删除首部和尾部空格

填充字符到字符串中使总长度等于指定长度 PadLeft (总长度 , 字符)

Page 68: 第三章   面向对象的编程基础

683.2.2 String 类(续)PadRight (总长度 , 字符)

例如 :string str = "a";string str1 = str.PadLeft(5,'e'); Console.WriteLine(str1); // 结果为 eeeeastring str2 = str.PadRight(5,'e'); Console.WriteLine(str2); // 结果为 aeeee

Page 69: 第三章   面向对象的编程基础

693.2.3 StringBuilder 类 StringBuilder 类位于 System.Text 名称空间下,使用 StringBuilder 类每次重新生成新字符串时不是再生成一个新实例,而是直接在原来字符串占用的内存空间上进行处理,而且它可以动态的分配占用的内存空间大小。因此,在字符串处理操作比较多的情况下,使用 StringBuilder 类可以大大提高系统的性能。

Page 70: 第三章   面向对象的编程基础

70

3.2.4 DataTime 类和 TimeSpan 类 DateTime 类可以表示范围在 0001年 1月 1日午夜 12:00:00 到 9999 年 12月 31日晚上 11:59:59之间的日期和时间,最小时间单位等于 100毫微秒。 TimeSpan 类可以表示一个时间间隔。其范围可 以在 Int64.MinValue 到 Int64.MaxValue 之间。

Page 71: 第三章   面向对象的编程基础

713.2.5 Math 类 Math 类位于 System命名空间下,为三角函数、对数函数和其他通用数学函数提供常数和静态方法。例如:

int i = 10, j = -5; double x = 1.3, y = 2.7; double a = 2.0, b = 5.0; Console.WriteLine(string.Format("-5 的绝对值为 {0}", Math.Abs(j))); Console.WriteLine(string.Format("大于等于 1.

3 的最小整数为 {0}", Math.Ceiling(x))); Console.WriteLine(string.Format("小于等于 2.

7 的最大整数为 {0}", Math.Floor(y)));

Page 72: 第三章   面向对象的编程基础

723.2.5 Math 类(续) Console.WriteLine(string.Format("10 和 -5 的较大者为 {0}", Math.Max(i, j))); Console.WriteLine(string.Format("1.3 和 2.7 的较小者为 {0}", Math.Min(x, y))); Console.WriteLine(string.Format("2 的 5次方为

{0}", Math.Pow(a, b))); Console.WriteLine(string.Format("1.3 的四舍五入值为 {0}", Math.Round(x))); Console.WriteLine(string.Format("5 的平方根为

{0}", Math.Sqrt(b))); Console.ReadLine(); } }}