定义一个Dog类,包含了age,weight等属性,以及对这些属性操作的方法(如setAge, setWeight, getAge, getWeight)。实现并测试这个类。
时间: 2023-03-19 20:20:58 浏览: 123
类 Dog {
private int age;
private int weight;
public Dog(int age, int weight){
this.age = age;
this.weight = weight;
}
public void setAge(int age){
this.age = age;
}
public void setWeight(int weight){
this.weight = weight;
}
public int getAge(){
return this.age;
}
public int getWeight(){
return this.weight;
}
} // 测试类
public class Test {
public static void main(String[] args) {
Dog dog = new Dog(5, 30);
System.out.println("年龄:" + dog.getAge());
System.out.println("体重:" + dog.getWeight());
dog.setAge(6);
dog.setWeight(35);
System.out.println("年龄:" + dog.getAge());
System.out.println("体重:" + dog.getWeight());
}
} 结果:
年龄:5
体重:30
年龄:6
体重:35
阅读全文