#编程:异常抛出处理.throw(),try,catch语句
在程序处理时遇到错误.可以采用抛出异常.来让catch语句处理.这三者的关联如何来表达尼.
如C#编程:定义类成员-定义属性 这篇文章中说到的;
- throw new Exception("err");
抛出一个错误信息err
- try
- {
- //结合 C#编程:定义类成员-定义属性 这篇文章.当给Age属性赋值20时会出错这时抛出异常.后转到catrh.为什么要用try尼.try的用处是:怀疑要执行的代码可能出错用try处理.如果出错就跳转到catch.
- MyObj.Age = 20;
- }
- catch (Exception e)//Exception e的意思是捕获异常错误代码赋于e这个对象
- {
- Console.WriteLine(e.Message);
- }
这样整体就很清楚的理解了throw(),try,catch语句
C#编程, catch, throw, try, 异常, 抛出
类属性的定义与定义字段有些相似之处.但结构代码要多出不少.属性一般与一个私有字段相关联,以控制对这个字段的访问,此时get块可以直接返回该字段的值.
看代码示例:
- class MyBase
- {
- private int age;
- public int Age
- {
- get
- {
- return age;
- }
- set
- {
- age = value;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- MyBase MyObj = new MyBase();
- MyObj.Age = 20;
- Console.WriteLine(MyObj.Age);
- Console.ReadKey();
- }
- }
Read the rest of this entry »
C#编程, catch, throw, try, 定义类, 属性
C#编程:定义类成员-定义方法
接着总结定义类成员-定义方法.定义方法通常有四个关键字.来决定方法的使用区域.
关键字:virtual, abstract:, override, extern, static
1.virtual:方法可以重写
2.abstract:只用于抽象类中重写
3.override:方法重写了一个基类的方法
4.extern:方法定义放在其它地方
定义方法实例:
- class MyClass
- {
- public string GetString()
- {
- return "here is a string.";
- }
- }
使用此方法:先实例化对象
- static void Main(string[] args)
- {
- MyClass MyObj = new MyClass();
- Console.WriteLine(MyObj.GetString());
- Console.ReadKey();
- }
Read the rest of this entry »
abstract, C#编程, extern, override, virtual, 定义方法, 定义类, 继承