C# Primer Plus编程练习14.8

要求编一个Rainfall类和RainfallQuarterly类,联系的目的是使用属性器和索引器,以及运算符重载。在其中尝试使用了一下try-catch,遇到的问题在上一篇随笔里已经提到了。给数组赋值时不能使用foreach,因为它不允许修改数组值。其他的也没什么特别的。只是main过于零乱与简单。
 ps:程序里所有的int型改成uint比较合适。转换运算符对显式和隐式的声明我和答案相反,可能是角度不一样,并且具体实现也不一样。但public static implicit operator Rainfall(RainfallQuarterly rainfallQuarterly)声明为explicit更好,因为除以4会发生截断造成数据丢失。
None.gif using  System;
None.gif
None.gif
None.gif
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Rainfall
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int[] monthRainfall=new int[12];                    //每月降水量
InBlock.gif

InBlock.gif        
public Rainfall()                                            //默认构造函数,月降水量为0
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
for(int i=0;i<12;i++)
InBlock.gif                monthRainfall[i]
=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public Rainfall(params int[] monthRainfall)                    //构造函数,输入值为1至12月的降水量,若参数个数超过12则抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if(monthRainfall.Length>12)
InBlock.gif                
throw new ArgumentOutOfRangeException("一年只有12个月!");
InBlock.gif            
//使用Clone方法,this.monthRainfall=(int[])monthRainfall.Clone(); 
InBlock.gif            
//this.monthRainfall指向一个新的长为3的数组,this.monthRainfall.Length=3
InBlock.gif
            for(int i=0;i<monthRainfall.Length;i++)
InBlock.gif                
this.monthRainfall[i]=monthRainfall[i];
InBlock.gif            
//不能使用foreach,foreach不允许修改数组值
InBlock.gif
            for(int j=monthRainfall.Length;j<12;j++)
InBlock.gif                
this.monthRainfall[j]=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void BrowseAll()                                        //显示全部降水
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Console.WriteLine(
"一月降水量:{0}mm",monthRainfall[0]);
InBlock.gif            Console.WriteLine(
"二月降水量:{0}mm",monthRainfall[1]);
InBlock.gif            Console.WriteLine(
"三月降水量:{0}mm",monthRainfall[2]);
InBlock.gif            Console.WriteLine(
"四月降水量:{0}mm",monthRainfall[3]);
InBlock.gif            Console.WriteLine(
"五月降水量:{0}mm",monthRainfall[4]);
InBlock.gif            Console.WriteLine(
"六月降水量:{0}mm",monthRainfall[5]);
InBlock.gif            Console.WriteLine(
"七月降水量:{0}mm",monthRainfall[6]);
InBlock.gif            Console.WriteLine(
"八月降水量:{0}mm",monthRainfall[7]);
InBlock.gif            Console.WriteLine(
"九月降水量:{0}mm",monthRainfall[8]);
InBlock.gif            Console.WriteLine(
"十月降水量:{0}mm",monthRainfall[9]);
InBlock.gif            Console.WriteLine(
"十一月降水量:{0}mm",monthRainfall[10]);
InBlock.gif            Console.WriteLine(
"十二月降水量:{0}mm",monthRainfall[11]);
InBlock.gif            Console.WriteLine(
"平均降水量:{0}mm",Average);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int this[int index]                                    //只读索引,获取一个月的降水量,越界抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(index<1||index>monthRainfall.Length)
InBlock.gif                    
throw new ArgumentOutOfRangeException("索引越界!"); 
InBlock.gif                
return monthRainfall[index-1];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int Average                                            //只读属性,返回平均降水量
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int sum=0;
InBlock.gif                
foreach(int temp in monthRainfall)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sum
+=temp;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return sum/monthRainfall.Length;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static Rainfall operator+(Rainfall rainfall1,Rainfall rainfall2)        //重载+,将两个成员数组对应值相加得到新对象
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Rainfall rainfall
=new Rainfall(rainfall1[1]+rainfall2[1],rainfall1[2]+rainfall2[2],rainfall1[3]+rainfall2[3],rainfall1[4]+rainfall2[4],rainfall1[5]+rainfall2[5],rainfall1[6]+rainfall2[6],rainfall1[7]+rainfall2[7],rainfall1[8]+rainfall2[8],rainfall1[9]+rainfall2[9],rainfall1[10]+rainfall2[10],rainfall1[11]+rainfall2[11],rainfall1[12]+rainfall2[12]);
InBlock.gif            
return rainfall;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static bool operator>(Rainfall rainfall1,Rainfall rainfall2)            //重载>,<,全部以Average属性为比较依据
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if(rainfall1.Average>rainfall2.Average)
InBlock.gif                
return true;
InBlock.gif            
else 
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static bool operator<(Rainfall rainfall1,Rainfall rainfall2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(rainfall1.Average<rainfall2.Average)
InBlock.gif                
return true;
InBlock.gif            
else 
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static implicit operator Rainfall(RainfallQuarterly rainfallQuarterly)    //转换运算符,把RainfallQuarterly显式式转为Rallfall
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{                                                                                //不能把该方法放到RainfallQuarterly中,因为使用了私有变量rainfall.monthRainfall
InBlock.gif
            Rainfall rainfall=new Rainfall();
InBlock.gif            
for(int i=0;i<4;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rainfall.monthRainfall[i]
=rainfallQuarterly[i+1]/4;
InBlock.gif                rainfall.monthRainfall[i
+4]=rainfallQuarterly[i+1]/4;
InBlock.gif                rainfall.monthRainfall[i
+8]=rainfallQuarterly[i+1]/4;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return rainfall;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class RainfallQuarterly
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int[] rainfallSeason=new int[4];                                    //保存每季度的降水量
InBlock.gif
        public RainfallQuarterly()                                            //默认构造函数,季降水量为0
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
for(int i=0;i<4;i++)
InBlock.gif                rainfallSeason[i]
=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public RainfallQuarterly(params int[] rainfallSeason)                //构造函数,输入值为1至4季的降水量,若参数个数超过4则抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if(rainfallSeason.Length>4)
InBlock.gif                
throw new ArgumentOutOfRangeException("一年只有4个季度!");
InBlock.gif            
for(int i=0;i<rainfallSeason.Length;i++)
InBlock.gif                
this.rainfallSeason[i]=rainfallSeason[i];
InBlock.gif            
for(int j=rainfallSeason.Length;j<4;j++)
InBlock.gif                
this.rainfallSeason[j]=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void BrowseAllSeasons()                                        //显示全部降水
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Console.WriteLine(
"一季度降水量:{0}mm",rainfallSeason[0]);
InBlock.gif            Console.WriteLine(
"二季度降水量:{0}mm",rainfallSeason[1]);
InBlock.gif            Console.WriteLine(
"三季度降水量:{0}mm",rainfallSeason[2]);
InBlock.gif            Console.WriteLine(
"四季度降水量:{0}mm",rainfallSeason[3]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int this[int index]                                    //只读索引,获取一个季度的降水量,越界抛出异常
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(index<1||index>rainfallSeason.Length)
InBlock.gif                    
throw new ArgumentOutOfRangeException("索引越界!"); 
InBlock.gif                
return rainfallSeason[index-1];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static explicit operator RainfallQuarterly(Rainfall rainfall)        //转换运算符,把Rainfall显式式转为RainfallQuarterly,为防止溢出(实际上基本不会)
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{                                                                            //可以把该方法放到任意相关类中
InBlock.gif
            RainfallQuarterly rainfallQuarterly;
InBlock.gif            
int[] temp=new int[4];
InBlock.gif            
int sum=0;
InBlock.gif            
for(int i=0;i<4;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sum
=0;
InBlock.gif                
for(int j=i*3;j<(i+1)*3;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sum
+=rainfall[j+1];
ExpandedSubBlockEnd.gif                }

InBlock.gif                temp[i]
=sum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            rainfallQuarterly
=new RainfallQuarterly(temp);
InBlock.gif            
return rainfallQuarterly;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Test 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
public static int Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int[] array=dot.gif{0,1,2,3,4,5,6,7,8,9,10,11};
InBlock.gif            RainfallQuarterly rainfallQuarterly
=null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rainfallQuarterly
=new RainfallQuarterly(array);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(ArgumentOutOfRangeException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//Rainfall rainfall2=new Rainfall(array);
InBlock.gif
            Rainfall rainfall=null;
InBlock.gif            
//rainfall=rainfall1+rainfall2;
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rainfall
=new Rainfall(array);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(ArgumentOutOfRangeException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(e.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif            rainfall.BrowseAll();
InBlock.gif            RainfallQuarterly rainfallSeason
=(RainfallQuarterly)rainfall;
InBlock.gif            rainfallSeason.BrowseAllSeasons();
InBlock.gif            Rainfall rainfall2
=rainfallSeason;
InBlock.gif            rainfall2.BrowseAll();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
//结束程序
InBlock.gif            
//
InBlock.gif
            Console.WriteLine("\nThanks for using!\nPress Enter to cancle");
InBlock.gif            Console.ReadLine();
InBlock.gif
InBlock.gif            
return(0);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/BlueskyGreenearth/archive/2005/03/30/128671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值