已阅读5页,还剩39页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
入门 第五讲类 继承 作业4 Card javaConstructorprintOutPlayer javaConstructorplayFirst play winTurn Belote javamain Card类 publicclassCard intvalue Stringname intsuit Card构造函数 publicclassCard publicintvalue publicStringname publicintsuit publicCard intv Strings intn value v name s suit n Cardprintout publicclassCard publicintvalue publicStringname publicintsuit publicCard intv Strings intn value v name s suit n publicprintOut System out println value name suit Player类 publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards Player构造函数 publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicPlayer Strings name s cards newCard 13 score 0 counter 0 PlayeraddCard publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicCardaddCard Cardc cards counter c counter Belote类 publicclassBelote staticCardcards staticPlayerplayers Belote构造函数 publicclassBelote staticCardcards staticPlayerplayers publicBelote createplayersplayers newPlayer 4 players 0 newPlayer Adam players 1 newPlayer Bob players 2 newPlayer Charlie players 3 newPlayer Daniel 卡片排列 publicclassBelote staticCardcards staticPlayerplayers publicBelote intcount 0 distributecardsfor inti 0 i 4 i for intj 1 j 13 j players i addCard cards count count Belotemain publicclassBelote staticCardcards staticPlayerplayers staticpublicvoidmain String args createanewinstanceofBeloteBelotegame newBelote publicclassBelote staticCardcards staticPlayerplayers staticpublicvoidmain String args createanewinstanceofBeloteBelotegame newBelote loopthroughplayersfor inti 0 i 4 i for intj 0 j 13 j players i cards j printOut 空对象 空对象意味着什么都没有 删掉数组中的一个元素很容易 但是也意味着没有办法挽回 publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicCardplayFirst if counter 0 returnnull else counter returncards 0 错误 cards 0 maybenull publicclassPlayer publicCardplayFirst for inti 0 i cards length i if cards i null counter cards i null returncards i 错误 returnnull publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicCardplayFirst for inti 0 i cards length i if cards i null counter CardreturnedCard cards i cards i null returnreturnedCard 正确 returnnull 空指针异常 publicclassPlayer publicCardplayFirst for inti 0 i cards length i System out println cards i suit WRONG 当访问对象cards i 的变量suit的时候 cards i 可能是空对象 空指针异常 publicclassPlayer publicCardplayFirst for inti 0 i cards length i if cards i null System out println cards i suit ok publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicCardplay intsuit for inti 0 i cards length i if cards i null publicclassBelote staticCardcards staticPlayerplayers staticpublicvoidmain String args createanewinstanceofBeloteBelotegame newBelote playaslongasplayershavecardsintdone 0 while done 0 CardpCards newCard 4 pCards 0 players 0 playFirst intsuit pCard 0 suit 错误pCards 1 players 1 play suit pCards 2 players 2 play suit pCards 3 players 3 play suit if playedCards 0 null done 1 staticCardcards staticPlayerplayers staticpublicvoidmain String args createanewinstanceofBeloteBelotegame newBelote playaslongasplayershavecardsintdone 0 while done 0 CardpCards newCard 4 pCards 0 players 0 playFirst if pCards 0 null intsuit pCard 0 suit OKpCards 1 players 1 play suit else done 1 完成一轮 publicclassPlayer publicStringname publicintscore publicintcounter publicCard cards publicvoidwinTurn Cardc1 Cardc2 Cardc3 Cardc4 score c1 value c2 value c3 value c4 value 作业4 总结 Makelittlesteps检查空指针注意实例和对象的区别 实例和对象 Bicyclebike1 newBicycle Bicyclebike2 newBicycle 实例和对象 Bicyclebike1 newBicycle Bicyclebike2 bike1 抽象 对象是抽象的工具抽象是计算机科学中的一个基本概念 抽象 在Java中 对象是类的一个实例类中包含变量和方法 这些是对象的本质对象使用new进行实例化 抽象为什么很重要 模块化 将一个大问题细分为一个个的小问题 很好的展现了现实世界的问题 继承 在生活中 我们继承了父母的特征在计算机科学中 对象从父类继承了变量和方法 继承 当你在Java中定义一个类时 你也可以将它定义为另一类的子类例如 类Bike Car和Motorcycle可以作为类Vehicle的子类 为什么要继承 避免代码的重复增强程序的模块化 重复的代码 publicclassBike publicintspeed publicvoidspeedUp ints speed s publicclassCar publicintspeed publicvoidspeedUp ints speed s 模块化 继承规则 子类继承父类所有的变量和方法使用关键字extends来表明一个类是从另一个类继承而来在子类的构造函数中使用关键字super调用父类的构造函数 案例 publicclassVehicle publicintnWheels numberofwheelspublicVehicle intn nWheels n intgetNWheels returnnWheels 案例 publicclassCarextendsVehicle publicCar intn super n publicvoidopenWindow 案例 publicclassBicycleextendsVehicle publicBicycle intn super
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电气管理考核制度
- 电子商务客户关系管理(AIGC版) 课件 第7、8章 电子商务客户服务管理体系构建、电子商务智能客服系统应用
- 2026年工程施工技术交底创新方法模拟试卷
- 基孔肯雅热防控责任落实测试题库单选题及答案解析
- 第7讲 习题课一匀变速直线运动推论和应用(学生版)
- 2026年注册水利水电工程师考试题库及答案
- 2025年山西省晋中市检察官、法官入员额考试真题(附答案)
- 2026年自考03205急救护理学试题及答案
- 2026年事业编医疗类中医骨伤模拟试卷
- 2025浙江嘉兴市海宁市黄湾村级创业创新投资开发有限公司招聘1人笔试历年备考题库附带答案详解
- 2025至2030年中国晾烟市场深度分析及投资战略咨询报告
- 2025新版现代企业管理试题库与参考答案2
- 新版22G101系列钢筋图解工程应用培训试题及答案
- 乡镇灾害信息员培训课件
- 国家车联网产业标准体系建设指南(智能交通相关)
- 农田用水管理办法
- 2025年长师单招校考题库及答案
- 瓦斯隧道施工培训课件
- DB3713-T 295-2023 金蝉人工养殖技术规程
- 2025至2030年中国团状模塑料(BMC) 行业市场竞争态势及投资前景研判报告
- 【天润乳业公司基于杜邦分析的盈利能力浅析(14000字论文)】
评论
0/150
提交评论