可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt

上传人:小飞机 文档编号:1312548 上传时间:2022-11-07 格式:PPT 页数:27 大小:515.81KB
返回 下载 相关 举报
可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt_第1页
第1页 / 共27页
可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt_第2页
第2页 / 共27页
可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt_第3页
第3页 / 共27页
可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt_第4页
第4页 / 共27页
可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt》由会员分享,可在线阅读,更多相关《可视化程序设计语言II——第3讲C#异常捕获与面向对象特性课件.ppt(27页珍藏版)》请在三一办公上搜索。

1、第3讲 C#异常捕获与面向对象特性,第3讲 C#异常捕获与面向对象特性www.robogix.c,大纲,1. 异常处理 2. try-catch-finally结构 3. C#面向对象程序设计基础 4. 继承5. 类的访问修饰符 6. this static 关键字 7. 多态(Polymorphism) 和虚方法, 2 ,大纲1. 异常处理 2 ,异常处理,C#的异常可能由两种方式导致:throw语句无条件抛出异常。C#语句和表达式执行过程中激发了某个异常的条件,使得操作无法正常结束,从而引发异常。例如整数除法操作分母为零时将抛出一个异常。异常由try语句来处理,try语句提供了一种机制来捕

2、捉执行过程中发生的异常。Try语句有3种基本格式:try-catchtry-finallytry-catch-finally, 3 ,异常处理 C#的异常可能由两种方式导致: 3 ,try-catch结构,案例名称:使用try-catch语句程序名称:2-21.csusing System;class Sample public static void Main(string args) long factorial=1; long num=Int64.Parse(args0); try checked / 计算数num的阶乘 for (long cur=1;cur=num;cur+) fac

3、torial*=cur; catch (OverflowException oe) Console.WriteLine(计算0的阶乘时引发溢出异常,num); Console.WriteLine(0,oe.Message); return; Console.WriteLine(0的阶乘是1,num,factorial); ,Checked的作用是溢出检查,溢出时报异常., 4 ,try-catch结构 案例名称:使用try-catch语句,try-finally结构,案例名称:使用try-finally语句程序名称:2-22.csusing System;public class Sample

4、 public static void Main() try Console.WriteLine(执行try子句!); goto leave; / 跳转到leave标签 finally Console.WriteLine(执行finally子句!); leave: Console.WriteLine(执行leave标签!); , 5 ,try-finally结构 案例名称:使用try-final,try-catch-finally结构,案例名称:使用try-catch-finally语句程序名称:2-23.csusing System;class Sample public static vo

5、id Main() try throw(new ArgumentNullException(); / 引发异常 catch(ArgumentNullException e) Console.WriteLine(Exception:0,e.Message); finally Console.WriteLine(执行finally子句); , 6 ,try-catch-finally结构 案例名称:使用try,C#面向对象程序设计基础,与传统的面向过程的编程方法相比,面向对象编程方法有3个优点:(1)程序的可维护性好;(2)程序容易修改;(3)对象可以使用多次,可重用性好。, 7 ,C#面向对象程

6、序设计基础 与传统的面向过程的编程方法相比,面,类的定义,class Aclass B void f() A a=new A(); , 8 ,类的定义 class A 8 ,继承,为了提高软件模块的可重用性和可扩充性,以便提高软件的开发效率,希望能够利用前人或自己以前的开发成果,任何面向对象的程序设计语言都能够提供两个重要的特性:继承性(inheritance)多态性(polymorphism), 9 ,继承 为了提高软件模块的可重用性和可扩充性,以便提高软件的开,使用继承,案例名称:使用继承程序名称:2-24.csusing System;class BaseA public void Fu

7、ncA() System.Console.WriteLine( Funciton A ); class DerivedA : BaseA public void FuncB() System.Console.WriteLine( Function B ); class Tester public static void Main( string args ) DerivedA aDerived = new DerivedA(); aDerived.FuncA(); aDerived.FuncB(); , 10 ,使用继承案例名称:使用继承 10 ,类的访问修饰符,案例名称:类的访问修饰符程序名

8、称:2-25.csusing System;class Class1 public string s; / 公有成员 protected int i; / 保护成员 private double d; / 私有成员 public void F1() s=Welcome six!; / 正确,允许访问自身成员 i=100; / 正确,允许访问自身成员 d=18.68; / 正确,允许访问自身成员, 11 ,类的访问修饰符 案例名称:类的访问修饰符 11 ,构造函数和析构函数,构造函数用于执行类的实例的初始化。每个类都有构造函数,即使没有声明它,编译器也会自动提供一个默认的构造函数。在访问一个类的

9、时候,系统将最先执行构造函数中的语句。使用构造函数请注意以下几个问题:一个类的构造函数与类名相同构造函数不声明返回类型。构造函数总是public类型的。, 12 ,构造函数和析构函数 构造函数用于执行类的实例的初始化。每个类,案例名称:构造函数和析构函数程序名称:2-26.csusing System;class Desk/构造函数和类名一样,析构函数前面加 public Desk()Console.WriteLine(Constructing Desk);weight=6;high=3;width=7;length=10;Console.WriteLine(0,1,2,3,weight,hi

10、gh,width,length);Desk()Console.WriteLine(Destructing Desk );protected int weight;protected int high;protected int width;protected int length;public static void Main()Desk aa=new Desk();Console.WriteLine(back in main() ); ;, 13 ,案例名称:构造函数和析构函数 13 ,this关键字,案例名称:使用this关键字程序名称:2-27.csusing System;public

11、 class Employee public string name; / 员工姓名 public decimal salary; / 员工薪水 / 构造函数 public Employee(string name, decimal salary) / 用this关键字给正在构造的对象的name和salary赋值 this.name = name; this.salary = salary; / 显示员工姓名及薪水 public void DiaplayEmployee() Console.WriteLine(姓名:0,name); Console.WriteLine(薪水:0元,salary

12、); / 用this方法将当前对象传给Tax.CalcTax()方法 Console.WriteLine(个人所得税:0元,Tax.CalcTax(this); public class Tax public static decimal CalcTax(Employee E) return (0.14m*(E.salary-800.0m); public class Sample public static void Main() / 声明类Employee的实例e Employee e = new Employee(小刘,4123.6m); e.DiaplayEmployee(); / 显

13、示员工姓名和薪水 , 14 ,this关键字 案例名称:使用this关键字 14 ,关键字static,案例名称:使用static关键字程序名称:2-28.csusing 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

14、c = new Person();Console.WriteLine (Person.total);, 15 ,关键字static 案例名称:使用static关键字 15,案例名称:使用静态方法程序名称:2-29.csusing 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 s

15、tatic void Main() Console.WriteLine (Person.getTotalPerson();Person p1 = new Person();Console.WriteLine (Person.getTotalPerson();, 16 ,案例名称:使用静态方法 16 ,C#面向对象高级特性,和其他的面向对象语言一样,C#支持多态、虚方法、函数的重载等。除此之外,C#还提供一种特殊的数据形态“装箱”, 17 ,C#面向对象高级特性 17 ,多态(Polymorphism),在C#中,多态性的定义是:“同一操作作用于不同的类的实例,不同的类将进行不同的解释,最后产生

16、不同的执行结果”。C#支持两种类型的多态性。编译时的多态性:编译时的多态是通过重载来实现的。对于非虚的成员来说,系统在编译时,根据传递的参数、返回的类型等信息决定实现何种操作。运行时的多态性:运行时的多态性是直到系统运行时,才根据实际情况决定实现何种操作。C#中,运行时的多态性通过虚方法实现。编译时的多态性提供了运行速度快的特点,而运行时的多态性则带来了高度灵活和抽象的特点。, 18 ,多态(Polymorphism) 在C#中,多态性的定义是:,虚方法,案例名称:使用虚方法程序名称:2-30.csusing System;class Teststatic void Main(string a

17、rgs)Base b = new Base();b.Draw();Derived d = new Derived();d.Draw();d.Fill();Base obj = new Derived();obj.Fill();obj.Draw();class Basepublic void Fill()System.Console.WriteLine(Base.Fill);public virtual void Draw() System.Console.WriteLine(Base.Draw);class Derived : Basepublic override void Draw()Sy

18、stem.Console.WriteLine(Derived.Draw);public new void Fill()System.Console.WriteLine(Derived.Fill);, 19 ,虚方法 案例名称:使用虚方法 19 ,函数重载,案例名称:重载普通函数程序名称:2-32.csusing System;class Overload public void Func() System.Console.WriteLine( Func() ); public void Func( int x, int y ) System.Console.WriteLine( Func( i

19、nt x, int y ) ); public void Func( long x, long y ) System.Console.WriteLine( Func( long x, long y ) ); public static void Main( string args ) Overload myOverload = new Overload(); myOverload.Func(); myOverload.Func(1,1); myOverload.Func(1L, 1L); / 会调用哪个重载函数呢? myOverload.Func(1L,1); , 20 ,函数重载 案例名称:

20、重载普通函数 20 ,案例名称:重载构造函数程序名称:2-33.csusing System;class CtorOverloadDemo static void Main(String args) MyInt i = new MyInt(2); System.Console.WriteLine(i.i); MyInt j = new MyInt(); System.Console.WriteLine(j.i); class MyInt public int i; public MyInt() i = 0; public MyInt( int i ) this.i = i; , 21 ,案例名

21、称:重载构造函数 21 ,装箱,案例名称:使用装箱程序名称:2-34.csusing System;public class UnboxingTest public static void Main( ) int i = 123; /装箱 object o = i; / 拆箱,必须是显示转换 int j = (int) o; Console.WriteLine(j: 0, j); , 22 ,装箱 案例名称:使用装箱 22 ,常用的几个函数,字符串输出把数据转化成字符串以作为输出实际上是很简单的。数值类型转换为字符串可以用int intAge=25;string strAge=intAge.T

22、oString();ToString()函数可以应用于所有数据类型.NET Framework基类库函数。这是因为ToString()是.NET FrameWork库System.Object的基类的函数之一。, 23 ,常用的几个函数字符串输出 23 ,转换函数, 24 ,转换函数函数功能Convert.ToBoolean()转换成,字符串处理函数,案例名称:使用字符串函数程序名称:2-35.csusing System;public class UnboxingTest public static void Main( ) string strDate = 2005-10-1 10:10:

23、55; string strDay = strDate.Substring(0,strDate.IndexOf( ); Console.WriteLine(输出日期: 0, strDay); , 25 ,字符串处理函数 案例名称:使用字符串函数 25 ,小结,本章介绍C#编程基础,掌握C#编译器的使用方法,掌握C#的数据结构,熟悉值类型和引用类型的使用方法。掌握操作符和控制语句的使用,着重掌握C#的异常处理和C#面向对象编程特性,熟悉C#常用的几个函数。, 26 ,小结本章介绍C#编程基础,掌握C#编译器的使用方法,掌握C#,本章习题,2-1 简述C#语言的特点。2-2 如何读取命令行参数?用程序说明。2-3 比较基本数据类型和引用数据类型的区别。2-4 C#的访问修饰符有几种,各具有什么限定作用? 2-5 构造函数有什么作用?简述重载构造函数的好处。2-6 析构函数有什么作用?如何定义析构函数?2-7 如何访问基类的函数?2-8 C#中如何实现异常处理?, 27 ,本章习题2-1 简述C#语言的特点。 27 ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号