




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 孙月江syjsdut 1 第11章理解参数数组 本章内容使用Params关键字接受任意数量的参数使用Params关键字和object类型 接受任意类型 任意数量的参数比较获取参数数组的方法和获取可选参数的方法 重载是指在相同的作用域内 声明两个或多个同名的方法如果需要对不同类型的参数执行相同的操作 重载非常有用示例 classConsole publicstaticvoidWriteLine intparameter publicstaticvoidWriteLine doubleparameter publicstaticvoidWriteLine decimalparameter 假如发生变化的不是参数类型 而是参数的数量 重载就有些力不从心了例如 Sum方法publicstaticintSum inta intb intsumTotal sumTotal a b returnsumTotal 多参数publicstaticintSumThree inta intb intc publicstaticintSumFour inta intb intc intd publicstaticintSumTen inta intb intc intd inte intf intg inth inti intj 数组在方法中的使用 数组元素作为方法中的参数privatestaticdecimalMaxAmount decimala decimalb decimal person1Sales 40000 10000 25000 50000 33000 60000 decimal person2Sales 80000 3000 110000 40000 33000 59000 MaxArray person1Sales 0 person2Sales 0 01 usingSystem 02 03 classMaxSales04 05 publicstaticvoidMain 06 07 decimal person1Sales 40000 10000 25000 50000 33000 60000 08 decimal person2Sales 80000 3000 110000 40000 33000 59000 09 10 Console WriteLine Maxindividualsalesforeachofthefirstsixmonths 11 for inti 0 ib 21 returna 22 else23 returnb 24 25 Maxindividualsalesforeachofthefirstsixmonths Month1 80 000 00Month2 10 000 00Month3 110 000 00Month4 50 000 00Month5 33 000 00Month6 60 000 00 数组引用作为方法参数要指定一个方法接受数组对象引用类型的参数 需要在方法头中包含适当的形式参数 publicstaticintSum uint numbers 提供给方法调用的数组变量参数并不包含任何方括号 03 classAverageAgeCalculator04 05 publicstaticvoidMain 06 07 byte agesScoutCamp newbyte 4 08 byte agesSpaceShuttle newbyte 6 10 Console WriteLine Enteragesfor 0 scouts agesScoutCamp Length 11 for inti 0 i agesScoutCamp Length i 12 13 Console Write Enterageforscoutnumber 0 i 1 14 agesScoutCamp i Convert ToByte Console ReadLine 15 16 Console WriteLine Averageageofscouts AverageAge agesScoutCamp 17 18 Console WriteLine nEnteragesfor 0 astronauts agesSpaceShuttle Length 19 for inti 0 i agesSpaceShuttle Length i 20 21 Console Write Enterageforastronautnumber 0 i 1 22 agesSpaceShuttle i Convert ToByte Console ReadLine 23 24 Console WriteLine Averageageofastronauts AverageAge agesSpaceShuttle 25 27 publicstaticbyteAverageAge byte ages 28 29 intageSum 0 30 ThesumofallagesisassignedtoageSum31 for inti 0 i ages Length i 32 ageSum ages i 33 Calculatetheaverageageandreturnitbacktothecaller34 return byte ageSum ages Length 35 36 Enteragesfor4scouts Enterageforscoutnumber1 10Enterageforscoutnumber2 9Enterageforscoutnumber3 8Enterageforscoutnumber4 9Averageageofscouts 9Enteragesfor6astronautsEnterageforastronautnumber1 34Enterageforastronautnumber2 38Enterageforastronautnumber3 30Enterageforastronautnumber4 35Enterageforastronautnumber5 33Enterageforastronautnumber6 34Averageageofastronauts 34 使用数组作为方法的返回值 01 usingSystem 02 03 classReturnMaxSalesArray04 05 publicstaticvoidMain 06 07 decimal person1Sales 40000 10000 25000 50000 33000 60000 08 decimal person2Sales 80000 3000 110000 40000 33000 59000 09 decimal maxSales 10 11 maxSales MaxArray person1Sales person2Sales 12 Console WriteLine Maxindividualsalesforeachofthefirstsixmonths 13 for inti 0 i 6 i 14 15 Console WriteLine Maxsalesmonth 0 1 12 C i 1 maxSales i 16 17 19 privatestaticdecimal MaxArray decimal sales1 decimal sales2 20 21 decimal maxSales newdecimal sales1 Length 22 23 for inti 0 ib 33 returna 34 else35 returnb 36 37 11 1使用数组参数 示例 写一个方法判断作为参数传递的一组值中的最小值classUtil publicstaticintMin int paramList if paramList null paramList Length 0 thrownewArgumentException Util Min notenougharguments intcurrentMin paramList 0 foreach intiinparamList if i currentMin currentMin i returncurrentMin 为了使用Min方法来判断2个int值的最小值 需要如下修改int array newint 2 array 0 first array 1 second intmin Util Min array 为了使用Min方法来判断2个int值的最小值 需要如下修改int array newint 3 array 0 first array 1 second array 2 third intmin Util Min array 缺点 必须编写额外的代码填充传入的数组 11 1 1声明params数组 classUtil publicstaticintMin paramsint paramList codeexactlyasbefore Params关键字对Min的作用是 调用该方法时 可以传递任意数量的整数参数intmin Util Min first second 自动转换成 int array newint 2 array 0 first array 1 second intmin Util Min array 01 usingSystem 03 classSumCalculator04 05 publicstaticvoidMain 06 07 intresult1 result2 result3 result4 result5 08 int myNumbers 4 10 6 8 2 10 result1 Sum 3 5 11 result2 Sum 10 20 30 12 result3 Sum 1 2 3 4 5 6 7 8 9 10 13 result4 Sum myNumbers 14 result5 Sum 16 Console WriteLine nEndresults 0 1 2 3 4 17 result1 result2 result3 result4 result5 18 20 publicstaticintSum paramsint numbers 21 22 intsum 0 24 Console Write nThenumbersarraycontains 0 elements numbers Length 25 foreach intnumberinnumbers 26 27 Console Write 0 number 28 sum number 29 30 Console WriteLine nThesumis 0 sum 31 returnsum 32 33 Thenumbersarraycontains2elements 35Thesumis 8Thenumbersarraycontains3elements 102030Thesumis 60Thenumbersarraycontains10elements 12345678910Thesumis 55Thenumbersarraycontains5elements 410682Thesumis 30Thenumbersarraycontains0elements Thesumis 0Endresults 8 60 55 30 0 关于params注意的问题 只能为一维数组使用params关键字publicstaticintMin paramsint table 编译器错误int int int 等等格式是正确的不能依赖params关键字重载一个方法 params不能构成方法签名的一部分 compile timeerror duplicatedeclarationpublicstaticintMin int paramList publicstaticintMin paramsint paramList 不允许params数组指定ref或out修饰符 compile timeerrorspublicstaticintMin refparamsint paramList publicstaticintMin outparamsint paramList Params数组必须是方法的最后一个参数 这表明每个方法只能有一个params数组参数 compile timeerrorpublicstaticintMin paramsint paramList inti 19 非params方法总是优先于params方法 所以 如果愿意 仍然可以创建一个方法的重载版本publicstaticintMin intleftHandSide intrightHandSide publicstaticintMin paramsint paramList 如果调用Min方法时 传递两个参数 优先选择第一个版本 否则 使用第二个版本优点 可以避免编译器创建和填充太多的数据编译器会检测并拒绝任何可能有歧义的重载 例如 compile timeerrorpublicstaticintMin paramsint paramList publicstaticintMin int paramsint paramList 20 11 1 2使用paramsobject 数组 参数数组的类型也允许不固定C 中使用object类型的参数数组来声明一个方法 它能接收任意数量的object参数值classBlack publicstaticvoidHole paramsobject paramList 可以不向它传递任何实参 在这种情况下 编译器传递一个长度为0的object数组Black Hole 转换成Black Hole newobject 0 可以使用null作为实参 数组是引用类型 所以允许使用null初始化数组Black Hole null 可以向Black Hole方法传递一个实际的数组object array newobject 2 array 0 fortytwo array 1 42 Black
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四川省成都市天府新区2024-2025学年八年级下期学期末考试数学试卷(含答案)
- 汉字收集资料课件
- 北师大版五年级上册数学第一单元 小数除法 检测卷(无答案)
- 2025年黑龙江省佳木斯市二十中中考数学二模试卷(含答案)
- 总承包合同(合集15篇)
- 户口申请书15篇
- “一带一路”与中国企业社会责任知到智慧树答案
- 汉字书法课件模板楷书凌
- 汉堡店加盟商业模式
- 永州市教师消防知识培训课件
- 财务岗位招聘笔试题与参考答案
- 电动汽车V2G技术
- 田忌赛马 同步分层作业(含答案)
- 高三年级年级主任工作计划
- 2023风光互补路灯设计方案
- jgj592023安全检查标准完整版
- 关节松动技术-上肢关节松动术(运动治疗技术)
- 2024CSCO肿瘤患者静脉血栓防治指南解读
- 供应商改善计划表
- DB11-T 1253-2022 地埋管地源热泵系统工程技术规范
- 2022年临沧市市级单位遴选(选调)考试试题及答案
评论
0/150
提交评论