[英]Dog.java:16: error: incompatible types: unexpected return value
Can't figure it out...想不通...
class Dog {
int age;
public Dog(int dogsAge) {
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public void getAge() {
return age;
}
public static void main(String[] args) {
Dog spike = new Dog(999);
spike.bark();
spike.run(999);
}
}
I think the error is in the part return age;我认为错误在于零件退货年龄; . .
And sorry if i'm not descriptive enough, I am new to Java.对不起,如果我的描述不够充分,我是 Java 新手。
So the variable age
is int
that returned type should be int
instead of void
.所以变量age
是int
,返回类型应该是int
而不是void
。
public int getAge() {
return age;
}
void
means that no value is returned to caller side. void
表示没有值返回给调用方。
You need to change your method like this你需要像这样改变你的方法
public int getAge() {
return age;
}
This is a getter method for age
.这是age
的 getter 方法。 age is of type int
in your program,so instead of void
,return int and you will be good. age 在你的程序中是int
类型,所以不是void
,返回 int ,你会很好。 void
indicates that this method is not going to return anything. void
表示此方法不会返回任何内容。
you can't return a value if the method is void.如果该方法为空,则无法返回值。 change your getAge method to return an int.更改您的 getAge 方法以返回一个 int。
public int getAge()
{
return age;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.