java - 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);
通常人们会在类中添加函数来对此进行操作,如上所示。
推荐阅读
- scala - 将 Scala OOP 代码转换为函数式代码
- r - 使用 R 搜索和替换
- graph-databases - JanusGraph:请在“图表”中添加一个名为“ConfigurationManagementGraph”的键
- c# - 使用 Tesseract 和 OpenCV C# .NET 用数字对压缩严重的 JPG 进行 OCR
- android - 如何为启动画面使用不同的背景颜色?
- excel - VBA将值粘贴到最后一行下方的新工作表中
- html - 如何使用 widget_tweaks 自定义 django 注册表单的帮助文本?
- json-api - 如何在 JSON-API 响应中表示多个图像大小?
- azure-cosmosdb - CosmosDb OrderBy 数组中的第一项
- java - 表现明智;(单个事件侦听器)或(查询)firebase?