实验6-多态技术实验报告_第1页
实验6-多态技术实验报告_第2页
实验6-多态技术实验报告_第3页
实验6-多态技术实验报告_第4页
实验6-多态技术实验报告_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

福建工程学院&信息科学与工程学院 实验报告课程名称面向对象程序设计任课教师姓名潘万丁学号年级15级专业网络工程班级网工1502实验编号实验6实验名称多态技术实验内容:1乐器类的多态性请编码实现如下需求:(1)乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin)。(2)各种乐器的弹奏( play)方法各不相同。(3)编写一个测试类InstrumentTest,要求:编写方法playTest,对各种乐器进行弹奏测试;要依据乐器的不同,进行相应的弹奏;在main方法中进行测试。public class LoginDemo public static void main(String args) 源代码:package ceshi1;public class Instrument public void play()System.out.println(乐器:);public static void main(String args) / TODO 自动生成的方法存根/钢琴(Piano)类 class Piano extends Instrumentpublic void play()System.out.println(钢琴(Piano)!);/小提琴(Violin)类 class Violin extends Instrument public void play()System.out.println(小提琴(Violin)!); /测试类InstrumentTestpackage ceshi1;public class InstrumentTest public static void playTest(Instrument y) y.play(); public static void main(String args) / TODO 自动生成的方法存根 Instrument instrument = new Instrument();Piano piano = new Piano();Violin violin = new Violin();playTest(instrument);playTest(piano);playTest(violin); 2龙与地下城游戏的怪物设计开发需求(1)英雄具有的属性包括:姓名、职业、等级、血量、攻击力和防御力,具有的方法包括:显示属性、判定是否死亡、攻击。(2)怪物共有的属性包括:名称、血量、攻击力、防御力;共有的方法包括:显示属性、判定是否死亡、和攻击。(3)攻击的伤害值计算公式为:攻击者攻击力-被攻击者防御力,被攻击者的血量会要扣减调伤害值。(4)死亡判定规则为:血量小于等于0。(5)设置两种怪物:独眼巨人,树怪。(6)独眼巨人特有的属性有:武器(狼牙棒或钉锤);独眼巨人具有的攻击方法是:使用武器(狼牙棒或钉锤)攻击。(7)树怪特有的属性:高度;树怪的攻击方式是使用树枝缠绕攻击。(8)创建2只独眼巨人、1只树怪和1个英雄,英雄与3只怪物轮流攻击对方:英雄攻击时,随机选取1只怪物作为攻击对象;怪物攻击时,随机选取1只怪物攻击英雄。(9)当怪物全部死亡时,提示“英雄获胜!”;当英雄死亡时,提示“英雄阵亡!”。public class LoginDemo public static void main(String args) 源代码:String name;int hp;int att;int def;public Native(String name, int hp, int att, int def) = name;this.hp = hp;this.att = att;this.def = def;public abstract void attack(Native emey);public boolean dead()if (hp0)emey.hp = emey.hp - harm;System.out.println(++攻击++,造成伤害+harm+点!);elseSystem.out.println(++攻击++,++Miss);独眼巨人public class Clocy extends Native String weapon;public Clocy(String name, int hp, int att, int def,String weapon) super(name, hp, att, def);this.weapon = weapon;Overridepublic void attack(Native emey) / TODO Auto-generated method stubint harm;harm = this.att - emey.def;if(harm0)emey.hp = emey.hp - harm;System.out.println(独眼巨人:++使用+this.weapon+攻击++,造成伤害+harm+点!);elseSystem.out.println(独眼巨人:++攻击++,++Miss);public void disp()System.out.print(独眼巨人:+);super.disp();树怪类public class Tree extends Native int height;public Tree(String name, int hp, int att, int def, int height) super(name, hp, att, def);this.height = height;Overridepublic void attack(Native emey) / TODO Auto-generated method stubint harm;harm = this.att - emey.def;if(harm0)emey.hp = emey.hp - harm;System.out.println(树怪:++使用树枝缠绕攻击++,造成伤害+harm+点!);elseSystem.out.println(树怪:++攻击++,++Miss);public void disp()System.out.print(树怪:+);super.disp();游戏类import java.util.ArrayList;import java.util.Random;public class LoginDemo static void wait(int second) throws InterruptedExceptionThread.sleep(second * 1000);static void flight(Hero hero,ArrayList monsters) throws InterruptedExceptionint index;Random ran = new Random();Native monst;while(true)index = ran.nextInt(monsters.size();monst = monsters.get(index);wait(1);hero.attack(monst);if(monst.dead()System.out.println(怪物+ +死亡);monsters.remove(index);if(monsters.size()=0)System.out.println(英雄胜利!);break;index = ran.nextInt(monsters.size();monst = monsters.get(index);wait(1);monst.attack(hero);if(hero.dead()System.out.println(怪物胜利!);break;public static void main(String args) throws InterruptedException / TODO Auto-generated method stubHero hero = new Hero(胡英雄, 200, 60, 10, 骑士, 1);hero.disp();ArrayList monsters = new ArrayList();monsters.add(new Clocy(胡眼龙,100 , 15,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论