一个简单的类
- public class Human {
- //定义普通人的属性,姓名、年龄、出生年月
- private String name;
- private int age;
- private String birth;
-
- //提供初始化方法
- public Human(String name) {
- this.name = name;
- }
- public Human(String name, int age) {
- this(name);
- this.age = age;
- }
- public Human(String name, int age, String birth) {
- this(name, age);
- this.birth = birth;
- }
-
- //用set方法设置属性,用get方法读取属性
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return this.age;
- }
- public void setBirth(String birth) {
- this.birth = birth;
- }
- public String getBirth() {
- return this.birth;
- }
-
- //人类拥有走路和吃饭的方法
- public void run() {
- System.out.println(this.name + "正在走路!");
- }
- public void eat() {
- System.out.println(this.name + "正在吃饭!");
- }
-
- //打印相关信息的方法
- public void printName() {
- System.out.println("当前对象的姓名为" + this.name);
- }
- public void printAge() {
- System.out.println("当前对象的年龄为" + this.age);
- }
- public void printBirth() {
- System.out.println("当前对象的出生年月为" + this.birth);
- }
- }
复制代码
希望可以帮助到别人
|
|