前面学习了Java中的类的相关知识,这里通过几个简单的案例来进一步熟悉内容分
案例1:Dog类
属性:颜色,品种,姓名,年龄
方法:咬人,吃饭
class Dog {
private String name; // 狗的名字
private String color; // 狗的颜色
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age; // 狗的年龄
private String type; // 狗的品种
// 构造方法
public Dog() {
}
public Dog(String name, String color, int age, String type) {
this.name = name;
this.age = age;
this.color = color;
this.type = type;
}
// setter gette 方法
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {