英文:
return type required in Java
问题
这段代码的返回类型是void。
英文:
public Course(String cat,String breed,int age) {
this.Cat = cat;
this.Breed = breed;
this.Age = age; }
what would be the return type of this code?
Hey everyone! I've never taken Java before and it's required, so I need to complete this assignment. Please help me with this very simple question. Thank you so so sooo much in advance!
答案1
得分: 1
以下是翻译好的代码部分:
public class Main {
public static void main(String[] args) {
Course cat = new Course("cat", "tabby", 7);
if (cat.isTabby()) {
System.out.println("Orange");
} else {
System.out.println("Other color");
}
}
static class Course {
String Cat;
String Breed;
int Age;
public Course(String cat, String breed, int age) {
this.Cat = cat;
this.Breed = breed;
this.Age = age;
}
public boolean isTabby() {
if (Breed.equals("tabby")) {
return true;
}
return false;
}
}
}
请注意,我将双引号 "
替换为正常的双引号 "
,并修改了条件语句中的比较运算符,以确保代码正确运行。
英文:
Like this:
public class Main {
public static void main(String[] args) {
Course cat = new Course("cat", "tabby", 7);
if(cat.isTabby()) {
System.out.println("Orange");
}
else {
System.out.println("Other color");
}
}
static class Course {
String Cat;
String Breed;
int Age;
public Course(String cat,String breed,int age) {
this.Cat = cat;
this.Breed = breed;
this.Age = age;
}
public boolean isTabby() {
if(Breed=="tabby") {
return true;
}
return false;
}
}
}
There is no return type, as it is a constructor. Constructors are used to
"construct things", such as
Course cat = new Course("cat","tabby",6);
And normally people add functions in the class to operate on this, as I show above.
答案2
得分: -1
这是一个构造函数,用于实例化对象。它没有返回类型。
英文:
This is a constructor, which is used to instantiate Objects. It does not have a return type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论