[英]Java - how to use a value from a class property when calling a constructor
I have the following problem:我有以下问题:
Animal
, Cat
, and Bug
.Animal
、 Cat
和Bug
。color
and leg_number
on the relevant and necessary classes.color
和leg_number
。 Have them be initialized within a constructor.move
with the Cat
and Bug
classes.Cat
和Bug
类调用方法move
的功能。 Have the method return a string "I'm moving with leg_number
legs!", with the " leg_number
" being leg_number
as set on the class.leg_number
腿移动!”,其中“ leg_number
”是leg_number
中设置的leg_number
。Bird
.Bird
的新类。 Add the property wing_number
.wing_number
。 Add the functionality that would allow us to call a method move
with the Bird
class.Bird
类调用方法move
的功能。 Have the method return the string "I'm moving with leg_number
legs!"leg_number
腿移动!” if wing_number
doesn't have an applicable value.wing_number
没有适用的值。 If wing_number
does have an applicable value, return the string "I'm flying".wing_number
确实具有适用的值,则返回字符串“我正在飞行”。 My code thus far is as follows:到目前为止,我的代码如下:
class Animal {
protected String color;
protected int leg_number;
public Animal(String color, int leg_number) {
this.color = color;
this.leg_number = leg_number;
}
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Cat extends Animal {
String color;
int leg_number;
public Cat(String color, int leg_number) {
super(color, leg_number);
this.color = "orange";
this.leg_number = 4;
}
@Override
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Bug extends Animal {
String color;
int leg_number;
public Bug(String color, int leg_number) {
super(color, leg_number);
this.color = "green";
this.leg_number = 16;
}
@Override
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Bird extends Animal {
String color;
int leg_number;
int wing_number;
public Bird(String color, int leg_number, int wing_number) {
super(color, leg_number);
this.color = "yellow";
this.leg_number = 2;
this.wing_number = wing_number;
}
public void move(int wing_number) {
if (wing_number > 0) {
System.out.println("I'm flying");
} else {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
}
I think I did what the instructions called for in setting the value for leg_number in the constructor.我想我按照说明在构造函数中设置 leg_number 的值做了什么。 I'm not sure how to use it in the constructor call, though.
不过,我不确定如何在构造函数调用中使用它。 It seems that if I have
Animal myCat = new Cat("orange", 4);
看来,如果我有
Animal myCat = new Cat("orange", 4);
that I'm not following the instructions for the assignment.我没有按照作业的说明进行操作。 But I'm not sure what else to put there.
但我不知道还有什么可以放在那里。 I've tried using
(color, leg_number)
and (this.color, this.leg_number)
, but as expected, they didn't work (I assume because the object hasn't been instantiated yet).我试过使用
(color, leg_number)
和(this.color, this.leg_number)
,但正如预期的那样,它们不起作用(我假设是因为对象尚未实例化)。 I tried setting the values in the parameter list of the constructor itself, but that didn't work either.我尝试在构造函数本身的参数列表中设置值,但这也不起作用。 I tried having the constuctor take no parameters, but I have to call the
super
constructor which does require parameters, so I get an error that way.我尝试让构造函数不带参数,但我必须调用确实需要参数的
super
构造函数,所以我会以这种方式得到错误。
What am I missing here?我在这里缺少什么?
EDIT: Where I'm trying to instantiate these is in the Main method.编辑:我试图在 Main 方法中实例化这些。
You don't need String color
and int leg_number
in your sub-classes.您的子类中不需要
String color
和int leg_number
。 You also don't need to override move()
method in all sub-classes, except Bird
.您也不需要覆盖所有子类中的
move()
方法,除了Bird
。 Additionally, your move()
method in Bird
shouldn't take any parameters but use wing_number
property instead.此外,您在
Bird
move()
方法不应采用任何参数,而应使用wing_number
属性。 Having said that, try the following:话虽如此,请尝试以下操作:
class Animal {
protected String color;
protected int leg_number;
public Animal(String color, int leg_number) {
this.color = color;
this.leg_number = leg_number;
}
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Cat extends Animal {
public Cat(String color, int leg_number) {
super(color, leg_number);
}
}
class Bug extends Animal {
public Bug(String color, int leg_number) {
super(color, leg_number);
}
}
class Bird extends Animal {
private int wing_number;
public Bird(String color, int leg_number, int wing_number) {
super(color, leg_number);
this.wing_number = wing_number;
}
@Override
public void move() {
if (wing_number > 0) {
System.out.println("I'm flying");
} else {
super.move();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.