簡體   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