- static void Main(string[] args)
- {
- //用数组弄出2个
- Sun sun = new Sun("吴沉烨", "女", "女人", 100); //子类
- Sun1 sun1 = new Sun1("小明", "男", "男人", 180);//子类
- Preson[] preson = { sun, sun1 }; //父类数组定义
- for (int i = 0; i < preson.Length; ++i) //数组里有几个元素循环多少次
- {
- for (int j = 1; j < 2; ++j) //这循环意义在于 catch中break的话,程序不会结束
- {
- try
- {
- if (preson[i] == ((Sun)preson[i])) //这肯定出错,所以try,然后跳到break,结束本次循环,进行下一个
- {
- ((Sun)preson[i]).Say();
- }
- }
- catch
- {
- break;
- }
- }
- for (int j = 1; j < 2; j++)//这循环意义在于 catch中break的话,程序不会结束
- {
- try
- {
- if (preson[i] == ((Sun1)preson[i]))//这肯定出错,所以try,然后跳到break,结束本次循环,进行下一个
- {
- ((Sun1)preson[i]).Say();
- }
- }
- catch
- {
- break;
- }
- }
- }
复制代码 |
|