定义一个Dog类,包含了age,weight等属性,以及对这些属性操作的方法(如setAge, setWeight, getAge, getWeight)。实现并测试这个类。
时间: 2023-03-19 13:20:58 浏览: 75
类 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
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)