1.老师,我的这个在eclipse上运行有结果,但是在网页提交后没有输出结果;
主要的问题集中在测试类中输出数组的问题上,希望老师和大神指点一下;
package com.imooc.animal1;
public class Animal {
// 属性:kind(种类)
private String kind;
// 无参构造
public Animal() {
System.out.println("动物都分种类");
}
// 创建带参(king为参数)构造函数
public Animal(String kind) {
super();
this.setKind(kind);
}
// 应用封装实现对私有属性的get/set操作
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
// 创建成员方法cry():void
public void cry() {
System.out.println("动物都有叫声");
}
}
package com.imooc.animal1;
public class Cat extends Animal {
public Cat() {
}
public Cat(String kind) {
super("猫");//问题2:为什么这样写了之后在测试类中不写种类的话Cryfangfa输出的是null?
}
@Override
public void cry() {
// TODO Auto-generated method stub
//super.cry("猫");问题1:这样写为什么是错的!!!!!
super.cry();
System.out.println(this.getKind() + "的叫声:喵喵喵");
}
}
package com.imooc.animal1;
public class Dog extends Animal {
public Dog() {
}
public Dog(String kind) {
super("狗");
}
@Override
public void cry() {
// TODO Auto-generated method stub
super.cry();
System.out.println(this.getKind() + "的叫声:汪汪汪");
}
}
package com.imooc.animal1;
public class Sheep extends Animal {
public Sheep() {
}
public Sheep(String kind) {
super("羊");
}
@Override
public void cry() {
// TODO Auto-generated method stub
super.cry();
System.out.println(this.getKind() + "的叫声:咩咩咩");
}
}
package com.imooc.test;
import java.util.Random;
import com.imooc.animal1.*;
public class AnimalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal one = new Animal();
one.cry();
Cat two = new Cat("猫");
two.cry();
Dog three = new Dog("狗");
three.cry();
Sheep four = new Sheep("羊");
four.cry();
// 生成父类对象数组,数组长度为5
Animal[] animalArray = new Animal[4];
// 产生随机数,随机产生三种具体子类的实例
/*
* 生成随机数的方法 int max=20; int min=10; Random random = new Random(); int s =
* random.nextInt(max)%(max-min+1) + min; System.out.println(s);
* random.nextInt(max)表示生成[0,max]之间的随机数,然后对(max-min+1)取模。
* 以生成[10,20]随机数为例,首先生成0-20的随机数,然后对(20-10+1)取模得到[0-10]之间的随机数,然后加上min=10,最后生成的是10-20的随机数
*/
// 循环输出,循环体中每个对象分别调用cry()方法。
/*
* 问题4:这样写对吗?为什么这样写羊的叫声没了?下面这样写就输出了要求的成果?
* if (s == 0) {
animalArray[0] = new Cat();
two.cry();
}
if (s == 1) {
animalArray[1] = new Dog();
three.cry();
}
if (s == 2) {
animalArray[2] = new Sheep();
four.cry();
}
*/
for (int i = 0; i <= animalArray.length+1; i++) {
int max = 2;
int min = 0;
Random random = new Random();
int s = random.nextInt(max)%(max-min) + min;
if (s == 0) {
animalArray[0] = new Cat("猫");
//animalArray[0].cry();问题3:为什么这样写输出结果为空
//two.cry();
animalArray[0].cry();
}
if (s == 1) {
animalArray[1] = new Dog("狗");
animalArray[1].cry();
}
if (s == 2) {
animalArray[2] = new Sheep("羊");
animalArray[2].cry();
}
}
}
}42
收起
正在回答
1回答
1、测试了你的代码,运行效果如下图,请注意图中的提示问题。

2、对于问题1:因为Animal类定义的cry()方法是没有参数的,所以在Cat类中如果用super.cry(”猫“)来调用此方法是错误的。
3、对于问题2:测试类中创建Cat类对象时,如果不传入种类参数,会调用Cat无参构造和Animal类的无参构造,没有给属性kind赋值,所以输出的种类是null。
4、对于问题3:animalArray[0]正常实例化后,animalArray[0].cry()这句是可以输出猫的叫声的,你说的输出的是空是什么意思呢?是否正常完成了实例化?
5、对于问题4:问题4下面写法不对,需要通过带参构造方法实例化或者用无参构造方法实例化之后,通过属性赋值确定动物种类,同时也要用对应的实例化对象去调用cry()方法,比如你的代码中的:animalArray[0] = new Cat(); two.cry();此处就应该用animalArray[0] = new Cat("猫"); animalArray[0]来调用cry()方法。
最后,注释和问题建议分开写,以免问题会丢失,祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星