【黑马程序员】C#面向对象中一些知识点总结和备忘

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------


异常处理

try catch

try 中一旦出错,则立即停止运行,进入catch中继续进行
比如:

try

    consonle.writeline("aaaaaaaaa");
    int a=convert.toint32("abc");
    consonle.writeline("bbbbbb");

catch
{
    consonle.writeline("cccccccc");
}


则运行结果是:
aaaaaaa
ccccccc

try

    consonle.writeline("aaaaaaaaa");
    int a=convert.toint32("123");
    consonle.writeline("bbbbbb");

catch
{
    consonle.writeline("cccccccc");
}

则运行结果是:
aaaaaaa
bbbbbbb
ccccccc


另外可以使用 Exception ex
例如:

try

    consonle.writeline("aaaaaaaaa");
    int a=convert.toint32("abc");
    consonle.writeline("bbbbbb");

catch(Exception ex)
{
    consonle.writeline("cccccccc"+ex.Message);
}

则可以得到捕获的异常的原因。
同理,还可以有 ex.StackTrace获得异常的堆栈,从而得知发生错误的行数。


同时,还可以自定义异常,比如对年龄进行自定义异常:

public string GetAgeDesc(int age)
{
    if(age<0)
    {
        throw new Exception("未出生的年龄啊!");
    }
    else if (age >150)
    {
        throw new Exception("超过世界纪录了!");        
    }
    else
    {
        retun "年龄正常";
    }
}

然后

try
{
    string desc =GetAgeDesc(300);
}
catch (Exception ex)
{
    consonle.writeline(""+ex.Message);
}


常量

主要用于
1,避免多个地方的同时修改造成的前后不一致。
2,避免修改或多次赋值造成的前后不一致
比如,取圆的周长和面积,会多次用到 π 。之前可能精度精确到个位,所以都是用的3,而后来精确到小数点后2位,就必须有很多地方从3改为3.14,但有了常量就只需要改常量值,之后用到的地方用常量名即可。
而常量不能再次赋值,就不会有问题了


索引器

关键字是this 。

class Program
    {
        static void Main(string[] args)
        {
            IndexClass a = new IndexClass();
            a[0] = "陈三";
            a[1] = "戴四";
            a[2] = "笠五";
            Console.WriteLine("a[0]=" + a[0]);
            Console.WriteLine("a[1]=" + a[1]);
            Console.WriteLine("a[2]=" + a[2]);
            Console.ReadKey();
        }
    }
    class IndexClass
    {
        private string[] name = new string[10];
        public string this[int index]
        {
            get { return name[index]; }
            set { this.name[index] = value; }
        }
    }

索引器的索引值不受类型限制。用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值。
比如:
        public string this[string name]
        {
        ……
        }
也是可以的。

索引器允许重载,一个类可以有多个索引器。
比如:
        public string this[string a]
        {
        ……
        }
        public string this[int a]
        {
        ……
        }
也是可以的。

多参数索引器如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Study
{
    class Program
    {
        static void Main(string[] args)
        {
            ScoreIndex s = new ScoreIndex();
            s["张三", 1] = 90;
            s["张三", 2] = 100;
            s["张三", 3] = 80;
            s["李四", 1] = 60;
            s["李四", 2] = 70;
            s["李四", 3] = 50;
            Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]);
            Console.WriteLine("张三的所有成绩为:");
            ArrayList temp;
            temp = s["张三"];
            foreach (IndexClass b in temp)
            {
                Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
            }
            Console.ReadKey();
        }
    }
    class IndexClass
    {
        private string _name;
        private int _courseid;
        private int _score;
        public IndexClass(string _name, int _courseid, int _score)
        {
            this._name = _name;
            this._courseid = _courseid;
            this._score = _score;
        }
        public string Name
        {
            get { return _name; }
            set { this._name = value; }
        }
        public int CourseID
        {
            get { return _courseid; }
            set { this._courseid = value; }
        }
        public int Score
        {
            get { return _score; }
            set { this._score = value; }
        }
    }
    class ScoreIndex
    {
        private ArrayList arr;
        public ScoreIndex()
        {
            arr = new ArrayList();
        }
        public int this[string _name, int _courseid]
        {
            get
            {
                foreach (IndexClass a in arr)
                {
                    if (a.Name == _name && a.CourseID == _courseid)
                    {
                & 

                    }

                }

             }

            .......

          }




---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值