定义一个有关人的Person类,内含属性: String name、int age、boolean gender、int id,所有的变量必须为私有(private)。
时间: 2024-05-01 12:20:07 浏览: 135
好的,我按照您的要求定义一个Person类:
```java
public class Person {
private String name;
private int age;
private boolean gender;
private int id;
public Person(String name, int age, boolean gender, int id) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isGender() {
return gender;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public void setId(int id) {
this.id = id;
}
}
```
如您所述,这个类中的所有变量都是私有的,同时提供了公共的 getter 和 setter 方法,以便在需要时可以访问和修改这些变量。感谢您的提问!
阅读全文
相关推荐
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)