下面是动物类的代码示例:
public class Animal {
// 成员变量
private String name;
private int age;
private String color;
// 无参构造方法
public Animal() {}
// 带全部参数的构造方法
public Animal(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
// 成员方法
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 setColor(String color) {
this.color = color;
}
public String getColor() {
return this.color;
}
public void show() {
System.out.println("动物名: " + this.name + ", 年龄: " + this.age + ", 颜色: " + this.color);
}
}