第3章面向对象程序设计_第1页
第3章面向对象程序设计_第2页
第3章面向对象程序设计_第3页
第3章面向对象程序设计_第4页
第3章面向对象程序设计_第5页
已阅读5页,还剩75页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第3章 C#面向对象程序设计,学习内容与要点 类的创建与使用 类的继承与多态 接口创建与实现 泛型创建与实现 委托与事件,3.1 类,在面向对象程序设计中,类被作为一种数据结构,是包含数据成员、函数成员和嵌套类型的数据结构。 类声明 属性 类修饰符 class 类名 类体 类修饰符包括new、public、protected、internal、private、abstract和sealed。,例3-1 定义一个Person类,例 定义一个Person类 using System; public class Person private string name; private int age;

2、private long ID; public Person(string n, int a, long i) name = n;age = a;ID = i; public virtual void Display() Console.WriteLine(“Name : 0” , name); Console.WriteLine(“Age : 0” , age); Console.WriteLine(“ID : 0” , ID);,创建类实例,格式为: 类名 实例名 = new 类名(参数); 两种形式 Person myTest = new Person(); 或 Person myTes

3、t; myTest = new Person();,类成员,类成员分为两大类,类本身定义的成员和从基类继承来的成员。 类成员包括函数成员和数据成员。 1类成员访问修饰符 访问修饰符用于指定类成员的可访问性。 共有public、private、protected和internal四个类成员访问修饰符。,2静态成员与实例成员,当用static修饰符声明成员时,得到静态成员,静态成员属于类,为这个类的所有实例所共享 不用static修饰符声明的成员,称为实例成员,实例成员被类的实例所拥有。,例3-2 定义一个含有静态成员,定义一个含有静态成员和一个实例成员的student类。,using Syste

4、m; class Student int SNO;/实例成员,表示学生学号 static int count;/静态成员,计数学生人数 public Student(int s) SNO = s; count +; public void display() Console.WriteLine(count= 0,SNO = 1,count,SNO); class TestStudent public static void Main() Student s1= new Student(12); s1.display(); Student s2 = new Student(20); s2.dis

5、play();,构造函数和析构函数,构造函数用于为对象分配空间,完成初始化工作。构造函数特殊性表现在: 构造函数的名字必须与类名相同; 构造函数可以带参数,但没有返回值; 构造函数在对象定义时被自动调用; 如果没有给类定义构造函数,则编译系统会自动生成一个缺省的构造函数,其形式如下: public con() : base() 构造函数可以被重载,但不可以被继承。,实例构造函数用于创建和初始化实例,创建新对象时要调用实例构造函数,其格式为: 访问修饰符 类名(形参列表) 函数体 静态构造函数用于初始化静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用

6、静态构造函数。其格式为: static 类名() 函数体 ,析构函数主要用于释放类实例。析构函数特殊性表现在: 析构函数的名字与类名相同,但它前面加一个“”符号; 析构函数不能够带参数,也没有返回值; 当撤销对象时,自动调用析构函数; 析构函数不能被继承,也不能被重载。,例3-3 使用构造函数和析构函数,例 使用构造函数和析构函数 using System; class Complex double imag, real; public Complex()/无参数构造函数 /有两个参数的构造函数 public Complex(double r , double i ) imag = i; re

7、al = r; Complex()/析构函数 ,方法,方法是表现类或对象行为的成员函数。 1.方法声明 声明格式: 方法修饰符 返回类型 方法名(形参列表) 方法体 修饰符包括:new、public、protected、internal、private、static、virtual、sealed、override、abstract和extern。,2.方法参数,(1)值参数 未用任何修饰符声明的参数为值参数。 (2)引用参数 用ref修饰符声明的参数为引用参数。在方法中对引用参数的任何更改都会反映给实参。 (3)输出参数 用out修饰符声明的参数称为输出参数。如果希望函数返回多个值,可使用输出

8、参数。,例3-4 使用值参数,例 使用值参数 using System; class TestValue static void Swap(int a , int b ) int t;t = a;a = b;b = t; static void Main() int x =10, y = 20; Console.WriteLine(x = 0 , y = 1, x , y); Swap(x , y); Console.WriteLine(x = 0 , y = 1, x , y);,例3-5 使用引用参数,例 使用引用参数 using System; class TestValue stati

9、c void Swap(ref int a , ref int b ) int t;t = a;a = b;b = t; static void Main() int x =10, y = 20; Console.WriteLine(x = 0 , y = 1, x , y); Swap(ref x , ref y); Console.WriteLine(x = 0 , y = 1, x , y); ,例3-6 使用输出参数,例 使用输出参数 using System; class TestOut static int OutMultiValue(int a , out char b) b =

10、 (char)a; return 0; static void Main() int t = 65 , r;char m; r = OutMultiValue(t ,out m); Console.WriteLine(r = 0 , m = 1” , r , m); ,(4)参数数组,用params修饰符声明的变量称为参数数组,它允许向函数传递个数变化的参数。,例3-7 使用参数数组,例 使用参数数组 using System; class TestParams static void MutiParams(params int var) for(int i= 0 ;i var.Length

11、; i +) Console.WriteLine(“var0 = 1”, i , vari); static void Main() int arr = 10 , 20 ,30; MutiParams( arr );/有3个参数,参数为一维数组 MutiParams(100, 200 );/有2个参数 MutiParams(); /没有参数,3.静态方法和实例方法,用static修饰符声明的方法为静态方法,它不与实例相关联,它属于类。只需要类名和方法名就可调用静态方法 未用static修饰符声明的方法为实例方法。它与特定实例相关联。可以用this来访问该实例。实例方法可以访问类中的任何成员。,

12、例3-8 使用静态方法和实例方法,例 使用静态方法和实例方法 using System; class TestMethod static int x;/静态数据成员 int y;/非静态数据成员 static void A()/静态方法A x = 10;/正确,在静态方法中访问静态成员 y = 20; /错误,在静态方法中访问非静态成员 void B() x = 10;/正确,在实例方法中访问静态成员 y = 20; /正确,在实例方法中访问非静态成员 static void Main() TestMethod t = new TestMethod(); TestMethod.A();/使用类

13、名调用静态方法 t.B(); /使用实例调用实例方法,4.方法重载,它允许一个类中有同名的方法存在 为了区分这些同名方法,要求方法有不同的参数,要么参数个数不同,要么参数类型不同。,例3-9 使用方法重载,例 使用方法重载 using System; class TestMethod int square(int x) return x*x; double square(double x)return x*x; decimal square(decimal x) return x*x; static void Main() TestMethod t = new TestMethod(); Co

14、nsole.WriteLine(“The squre is 0 , 1 , 2”, t.square(10), t.square(12.34) , t.square(123.456m); ,5.运算符重载,运算符重载可以对C#中已有的运算符赋予新的功能。其格式为: public static 返回值类型 operator unary-operator ( 参数列表 ) 方法体 ,例3-10 重载Point类的和运算符,例3-10 重载Point类的和运算符 using System; class Point private int x,y; public Point(int a, int b)

15、x = a;y = b; public static Point operator +(Point p) p.x +;p.y +;return p; public void Display() Console.WriteLine(Point.x = 0, Point.y = 1,x,y); public static Point operator +(Point p1,Point p2) Point p = new Point(0,0);p.x = p1.x + p2.x;p.y = p1.y + p2.y; return p; static void Main(string args) Po

16、int a = new Point(10,20); Point b = new Point(30,40); a = a + b;a.Display(); a +;a.Display();,6.this关键字,this 关键字引用类的当前实例,成员通过this关键字可以知道自己属于哪一个实例。this关键字是一个隐含引用,它隐含于每个类的成员函数中。 this 的常用用途: 限定被相似的名称隐藏的成员 将对象作为参数传递到其他方法 声明索引器,字段与属性,1.字段 字段表示与对象或类相关联的变量。 静态字段:用static修饰符声明的字段,无论存在多少个类实例,它们都共享一个静态字段拷贝。 实例

17、字段:没用static修饰符声明的字段,类的每个实例都包含实例字段的一个拷贝。 只读字段:用readonly修饰符声明的字段,它只能在字段声明或构造函数中赋值,在其它任何地方都不能改变只读字段的值。,例3-11 使用字段,例 使用字段 using System; class Goods public double high; public readonly double width = 30; public static int count = 0; public Goods(double h,double w) high = h; width = w;count +; static void

18、 Main(string args) Goods y = new Goods(100,200); Console.WriteLine(high=0,width=1,count=2“ ,y.high,y.width,Goods.count); Goods z = new Goods(300,400); Console.WriteLine(high=0,width=1,count=2“ ,z.high,z.width,Goods.count);,2.属性,属性用于刻画对象的特征或表示对象的状态,它提供对类或对象性质的访问。 其格式为: 访问修饰符 数据类型 属性名 getget访问器代码块 set

19、set访问器代码块 set访问器:给属性赋值,它使用value设置属性的值 get访问器:获取属性值,它通过return返回属性的值。 如果只有get访问器,表示是只读属性;如果只有set访问器,表示只写属性;如果既有get访问器,也有set访问器,表示读写属性。,例3-12 使用属性,例 使用属性 class Window private double m_width = 30; public double width getreturn m_width; setm_width = value; static void Main(string args) Window y = new Win

20、dow(); y.width=200; Console.WriteLine(The width of window is 0.,y.width);,继承,继承是面向对象程序设计的一个重要特征,它允许在既有类的基础上创建新类,新类从既有类中继承类成员,而且可以重新定义或加进新的成员,从而形成类的层次或等级。 一般称被继承的类为基类或父类,而称继承后产生的类为派生类或子类。,C#继承有如下重要性质:,C#只允许单继承,即派生类只能有一个基类; C#的继承是可传递的,如果 C 从 B 派生,而 B 从 A 派生,那么 C 就会既继承在 B 中声明的成员,又继承在 A 中声明的成员; 派生类扩展它的直

21、接基类,即派生类可以添加新的成员,但不能删除从基类继承的成员; 构造函数和析构函数不能被继承; 派生类可以隐藏基类的成员,如果在派生类中声明了与基类同名的新成员时,基类的该成员在派生类中就不能被访问到。,1.派生类的声明,类修饰符 class 类名 :基类 类体 ,例3-13 Person派生新类Employee,例 从声明的Person类派生一个新类Employee public class Employee : Person private string department; private decimal salary; public Employee(string n , int a

22、 , long i , string d , decimal s) : base(n , a , i ) department = d; salary = s; public override void Display() base.Display(); Console.WriteLine(“Department : 0” , department); Console.WriteLine(“Salary : 0” , salary);,2.base关键字,base关键字用于从派生类中访问基类的成员,它有两种基本用法: 指定创建派生类实例时应调用的基类构造函数,用于调用基类的构造函数完成对基类成

23、员的初始化工作; 在派生类中访问基类成员。 3.成员隐藏 在派生类中,通过声明与基类同名的新成员可以隐藏基类的成员,例3-14 使用成员隐藏,例 使用成员隐藏 public class Employee : Person private string department; private decimal salary; public Employee(string n , int a , long i , string d , decimal s) : base(n , a , i ) department = d; salary = s; new public void Display()

24、 base.Display(); Console.WriteLine(“Department : 0” , department); Console.WriteLine(“Salary : 0” , salary);,多态,多态性是指不同对象收到相同消息时,会产生不同动作。从而实现“一个接口,多种方法”。 C#支持两种多态性: 编译时多态性,是在程序编译时就决定如何实现某一动作,是通过方法重载和运算符重载实现的。 是运行时多态性,是在运行时动态实现某一动作,是通过继承和虚成员实现的。,例3-15 通过虚方法实现多态性,例 通过虚方法实现多态性 using System; class Base

25、public void Display() Console.WriteLine(“Display in Base”); public virtual void Print() Console.WriteLine(“Print in Base”); class Derived : Base new public void Display()Console.WriteLine(“Display in Derived”); public override void Print() Console.WriteLine(“Print in Derived”); class TestVirtual sta

26、tic void Main() Base b = new Base();Derived d = new Derived(); b.Print();d.Print(); b.Display();d.Display(); b = d; b.Print();d.Print(); b.Display(); d.Display();,抽象类,抽象类表示一种抽象的概念,用来为它的派生类提供一个公共接口。 声明类抽象类:加上abstract修饰符 抽象类只能作为其它类的基类,不能实例化。 抽象类可以包含抽象方法和抽象访问器,也可以包含实例字段和非抽象方法,例3-16 使用抽象类,例 使用抽象类 abstra

27、ct class Figure protected double x = 0 , y = 0; public Figure(double a, double b)x =a; y = b; public abstract void Area(); class Square : Figure public Square(double a, double b):base(a,b) public override void Area() Console.WriteLine(The area of square is 0,x*y); class Circle : Figure public Circle

28、(double a):base(a,a) public override void Area() Console.WriteLine(The area of circle is 0,x*x*Math.PI ); class TestAbstract public static void Main() Square s = new Square(20,30); Circle c = new Circle(10); s.Area();c.Area(); ,密封类,不能被继承的类,称为密封类。 声明密封类:使用sealed修饰符 例3-17 使用密封类 sealed class SealedClas

29、s public double x = 0 , y = 0; public SealedClass(double a, double b)x =a;y = b; public void Display() Console.WriteLine(“x = 0 , y = 1”,x , y); ,静态类,类中只包含静态成员 声明静态类:使用static修饰符 例3-18 声明静态类 static class StaticClass public const int y=100; public static void Display(int x) Console.WriteLine(“x = 0,y=

30、0”,x,y); ,注意: 静态类既不能实例化,也不能被继承。 静态类默认继承自System.Object根类,不能显式指定任何其它基类。 静态类不能有实例构造函数,系统也不会为它提供默认构造函数。但它可以包含静态构造函数。 静态类不能有任何实例成员,但可以有常量成员。 静态类不能实现任何接口。 静态类的成员不能有protected或protected internal访问保护修饰符。 静态类的成员默认并不是static类型的,必须用static修饰符显式声明成员。,例3-20 使用静态类 class Program static void Main(string args) /正确的使用形式

31、 StaticClass.Display(200); /错误的使用形式,无法生成静态类的实例 StaticClass c = new StaticClass(); Console.Read(); ,分部类,使用partial 关键字声明类或结构,就可以将类的一部分放在一个文件中,而将另一部分放在另一个文件中 注意: 分部类支持类、结构和接口,但不支持委托和枚举 在分部类中,不能把同一个成员声明两次。 同一个类型的各个部分都必须包含partial修饰符。 同一个类型各个部分必须位于相同的名称空间中。 同一个类型的各个部分必须被同时编译。 如果一个类型是某个基类的派生类,则该类型的各个部分上指定的

32、基类必须一致。,例3-21 使用分部类,3.2 接口,接口并不提供实现。它只表示一种约定,实现接口的类或结构必须遵守该接口定义的约定。 接口声明格式: 接口修饰符 interface 接口名 : 基接口 接口体 ,例3-22 声明一个IShape接口,例声明一个IShape接口 public interface IShape double Area(); string Type get; 注意: 接口的所有成员都被定义为公有的,使用其它修饰符是错误的; 接口不能包含常量、域; 接口不能包含构造函数、析构函数和静态成员。,例3-23 实现IShape接口,接口实现 例实现IShape接口 pub

33、lic class Rectangle : IShape private int width, height; public Rectangle(int w, int h) width = w; height = h; public double Area() return width * height; public string Type get return 矩形; ,3.3 泛型,泛型即通过参数化类型来实现在同一份代码上操作多种数据类型。 1. 泛型类 定义格式: 修饰符 class 类名 类体 泛型类的实例化格式为: 类名 实例名=new 类名(构造函数的实参);,例3-24 堆栈泛

34、型类,例堆栈泛型类 class Stack private T s; int pos; public Stack(int size) s = new Tsize; pos = 0; public void Push(T val) spos = val; pos+; public T Pop() pos-; return spos; public void Display() Console.WriteLine(Stack Push:); foreach (T i in s) Console.WriteLine(i); ,class Program static void Main(string

35、 args) Stack s1 = new Stack(2); s1.Push(1); s1.Push(2); s1.Display(); Console.WriteLine(Stack Pop:); Console.WriteLine(s1.Pop(); Console.WriteLine(s1.Pop(); Stack s2 = new Stack(2); s2.Push(one); s2.Push(two); s2.Display(); Console.WriteLine(Stack Pop:n0n1, s2.Pop(),s2.Pop(); Console.Read(); ,泛型方法,使

36、用类型参数声明方法 例3-25在数组中查找某个数 class Program public static int Find(T values,T val) for(int i=0;ivalues.Length;i+) if(valuesi.Equals(val) return i; return -1; static void Main(string args) int val = 4; int pos = Program.Find(new int 1, 2, 3, 4, 5, 6 , val); Console.WriteLine(Found 0 in position of 1, val,

37、pos); ,3.4 委托与事件,委托属于引用类型,用于封装方法(函数)的引用 使用委托的步骤: 委托声明;委托实例化;委托调用。 委托声明的格式: 修饰符 delegate 返回值类型 标识符 (形参列表); 委托实例化用于创建委托实例。与类实例创建的语法相同,例3-28 冒泡法排序 public class Sort public delegate bool Compare(int a, int b); public static void BubbleSort(int element, Compare compare) for(int i=0;i i; j-) if (compare(e

38、lementi, elementj) Swap(ref elementi, ref elementj); private static void Swap(ref int a, ref int b) int temp; temp = a; a = b; b = temp; ,class Program static void Main(string args) int ElementArray = new int1023, 3, 58, 23, 90, 45, 12, 78, 25, 67; Select s = new Select(); Sort.Compare cp = new Sort.Compare(s.Ascending);Sort.BubbleSort(ElementArray, cp); s.Display(ElementArra

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论