繁体   English   中英

Java 中要求的返回类型

[英]return type required in Java

public Course(String cat,String breed,int age) {
    this.Cat = cat;
    this.Breed = breed;
    this.Age = age; }

这段代码的返回类型是什么?

大家好,Java我从来没有考过,这是必填项。 所以我需要完成这个任务。 请帮我解决这个非常简单的问题。 非常感谢你!

像这样:

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);

通常人们会在 class 中添加函数来对此进行操作,如我上面所示。

这是一个构造函数,用于实例化对象。 它没有返回类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM