在Java中需要返回类型

huangapple go评论54阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月29日 05:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63143336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定