Java中需要返回类型
此代码的返回类型是什么Java中需要返回类型,java,return,Java,Return,此代码的返回类型是什么 大家好!我以前从未学过Java,这是必修课,所以我需要完成这项作业。请帮我回答这个非常简单的问题。提前非常感谢你 这是一个构造函数,用于实例化对象。它没有返回类型。如下所示: public Course(String cat,String breed,int age) { this.Cat = cat; this.Breed = breed; this.Age = age; } 没有返回类型,因为它是构造函数。构造函数用于 “构造事物”,例如 通
大家好!我以前从未学过Java,这是必修课,所以我需要完成这项作业。请帮我回答这个非常简单的问题。提前非常感谢你 这是一个构造函数,用于实例化对象。它没有返回类型。如下所示:
public Course(String cat,String breed,int age) {
this.Cat = cat;
this.Breed = breed;
this.Age = age; }
没有返回类型,因为它是构造函数。构造函数用于
“构造事物”,例如
通常情况下,人们会在类中添加函数来对其进行操作,如我上面所示。这看起来像一个构造函数。构造函数没有返回类型。如何修复此问题?:/它说“无效的方法声明;需要返回类型”,您需要发布一个。如何修复此问题?:/它表示“无效的方法声明;需要返回类型”
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;
}
}
}
Course cat = new Course("cat","tabby",6);