麻烦老师看下我这个作业
package com.imooc.zoo; import java.util.Scanner; public class Test { public static void main(String[] args) { String i;//存放用户选项 //循环执行选项界面 for (String n="1"; n.equals("1");) { new Minu().choise(); Scanner sc = new Scanner(System.in); i = sc.next(); switch (i) { case "1": System.out.println(new Bear("Bill", 1));break; case "2": System.out.println(new Lion("Lain", 2, "灰色", "公狮"));break; case "3": System.out.println(new Monkey("tom", 1,"金丝猴"));break; case "4": System.out.println(new Parrot("Rose",1, "牡丹鹦鹉"));break; case "5": System.out.println(new Clown("kahle", "5年"));break; default:System.out.println("大吉大利,今晚吃鸡"); break; } //循环执行是否重复观看询问 do { System.out.println("*****是否继续观看(1/0)*****"); Scanner sc1 = new Scanner(System.in); n = sc.next(); //当输入值非1和0时 if (!n.equals("1") &!n.equals("0")) { System.out.println("******输入有误,请重新输入******"); } else if (n.equals("0")){ System.out.println("******欢迎再次收看******"); } } while (!n.equals("1") &!n.equals("0")); } } }
package com.imooc.zoo; public abstract class Animal { private String name; private int age; public Animal(){ } public Animal(String name,int age){ this.setName(name); this.setAge(age); } public abstract String love();//抽象方法要在抽象类下实现,抽象方法不需要大括号,在小括号后面加分号。 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.imooc.zoo; public interface IAct { public String skill(); public String act(); }
package com.imooc.zoo; public class Lion extends Animal implements IAct{ private String color; private String sex; public Lion(){ } public Lion(String name,int age,String color,String sex){ super(name,age); this.setColor(color); this.setSex(sex); toString(); } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String love(){ String str="喜欢吃肉类"; return str; } public String skill(){ String str="擅长钻火圈"; return str; } public String act(){ return null; } public String toString(){ String str="表演者:"+this.getName()+"\n年龄:"+this.getAge()+"岁"+"\n性别:"+this.getSex()+"\n毛色:"+this.getColor()+"\n技能:"+this.skill()+"\n爱好:"+this.love(); return str; } }
package com.imooc.zoo; public class Monkey extends Animal implements IAct{ private String type; public Monkey(){ } public Monkey(String name,int age,String type){ this.setAge(age); this.setName(name); this.setType(type); toString(); } public void setType(String type){ this.type=type; } public String getType(){ return type; } public String love(){ String str="喜欢模仿人的动作表情"; return str; } public String skill(){ String str="骑独轮车过独木桥"; return str; } public String act(){ return null; } public String toString(){ String str="表演者:"+this.getName()+"\n年龄:"+this.getAge()+"岁"+"\n品种:"+this.getType()+"\n技能:"+this.skill()+"\n爱好:"+this.love(); return str; } }
package com.imooc.zoo; public class Clown implements IAct { private String name; private String years; public Clown() { } public Clown(String name,String years) { this.setYears(years); this.setName(name); toString(); } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setYears(String years) { this.years = years; } public String getYears() { return years; } public String dress() { String str = "身着五彩服装,头上戴着彩色的帽子,脸上画着夸张的彩妆"; return str; } @Override public String skill() { String str = "脚踩高跷,进行杂技表演魔术"; return str; } @Override public String act() { return null; } public String toString() { String str = "表演者:" + this.getName() + "\n艺龄:" + this.getYears() + "\n着装:" + this.dress() + "\n技能:" + this.skill(); return str; } }
package com.imooc.zoo; public class Bear extends Animal implements IAct { public Bear(){ } public Bear(String name,int age){ this.setAge(age); this.setName(name); } @Override public String skill() { String str="挽着花篮,打着雨伞,自立走秀"; return str; } @Override public String act() { // TODO Auto-generated method stub return null; } @Override public String love() { String str="喜欢卖萌"; return str; } public String toString(){ String str="表演者:"+this.getName()+"\n年龄:"+this.getAge()+"岁"+"\n技能:"+this.skill()+"\n爱好:"+this.love(); return str; } }
package com.imooc.zoo; public class Minu { public void choise(){ System.out.println("********欢迎来到太阳马戏团*********"); System.out.println("********请选择表演者*********"); System.out.println("********1.棕熊*********"); System.out.println("********2.狮子*********"); System.out.println("********3.猴子*********"); System.out.println("********4.鹦鹉*********"); System.out.println("********5.小丑*********"); } }
package com.imooc.zoo; public class Parrot extends Animal implements IAct{ private String type; public Parrot(){ } public Parrot(String name,int age,String type){ this.setAge(age); this.setName(name); this.setType(type); toString(); } public void setType(String type){ this.type=type; } public String getType(){ return type; } public String love(){ String str="喜欢模仿人的动作表情"; return str; } public String act(){ return null; } public String skill(){ String str="骑独轮车过独木桥"; return str; } public String toString(){ String str="表演者:"+this.getName()+"\n年龄:"+this.getAge()+"岁"+"\n品种:"+this.getType()+"\n技能:"+this.skill()+"\n爱好:"+this.love(); return str; } }
哪些地方没写好,麻烦给修改意见
向上转型都没用到呢,这么重要的概念,还有什么final,default之类的。请问这个道题目是需要用到的吗?如果能用上,应该怎么写呢?
42
收起
正在回答
1回答
1)作业中要求是要用多态来实现 ,如果用多态来实现就需要在外面事先定义IACT iAct;。例如case 1:iAct = new Bear("Bill", 1);break; case 2:iAct = new Lion("Lail", 2, "灰色", "公狮");出switch后再iAct.act();。2)要实现动物的act( ),其实也就是toString( )中的内容。另:作业批复时,老师会根据你的代码情况,建议代码优化方案。同学还是有二次提交机会的哦。
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星