java - 一个方法的例子是 int age;通常低于班级?
问题描述
一个方法示例是 int age;低于班级?其他评论是否正确?
public class human { //just a class?
//this is a method?
int age;
String name;
String hairColor;
String gender;
public void speaking() { //this is the constructor?
System.out.println("my name is " + name);
System.out.println("my age is " + age + " years old");
System.out.println("my hair color is " + hairColor);
System.out.println("my gender is " + gender);
}
public static void main(String args[]) { //main is used to excute
human earthHuman = new human();//this is known as a object
//instances?
earthHuman.age = (18);
earthHuman.name = ("dan");
earthHuman.hairColor = ("black brownish");
earthHuman.gender = ("male");
earthHuman.speaking();
}
}
解决方案
public class human { //Here you declare a class
//This is a field with package-private (default) permissions
//might be also called a variable or a parameter
int age;
String name;
String hairColor;
String gender;
public human() { //This is a constructor
}
public void speaking() { //This is a method not a constructor
System.out.println("my name is " + name);
System.out.println("my age is " + age + " years old");
System.out.println("my hair color is " + hairColor);
System.out.println("my gender is " + gender);
}
//Main is the first method to be called when program executes, it is
//program's entry point
public static void main(String args[]) {
//This is an object better known as an instance
human earthHuman = new human();
//These are parametes
earthHuman.age = (18);
earthHuman.name = ("dan");
earthHuman.hairColor = ("black brownish");
earthHuman.gender = ("male");
//Calling method speaking on earthHuman instance
earthHuman.speaking();
}
}
推荐阅读
- doctrine-orm - Zend 框架 3 - 找不到教义实体
- ruby-on-rails - 分布式 Rails 应用程序 - 如何配置?
- c# - 保存文件并防止用户写入
- javascript - 如何限制在 react js 的输入类型编号字段中输入多个点?
- python - UnboundLocalError:局部变量“x”
- sails.js - Sails.JS 模型无法全局访问,即使 Globals.JS 包含模型:true
- angular - url参数错误时如何触发通配符路由?
- java - IntelliJ 在我的有效字符串上报告“非法十六进制转义序列”
- javascript - Zapier - Catch Hook - JSON 数组 - 遍历数组中的每个项目
- html - 如何从nodejs创建和存储html页面?