编写一个java版本的程序,管理小区
时间: 2023-01-11 21:55:36 浏览: 77
这是一个可以作为管理小区的 Java 程序的示例:
```
import java.util.ArrayList;
import java.util.List;
public class Community {
private String name;
private List<Resident> residents;
public Community(String name) {
this.name = name;
this.residents = new ArrayList<>();
}
public String getName() {
return name;
}
public List<Resident> getResidents() {
return residents;
}
public void addResident(Resident resident) {
residents.add(resident);
}
public void removeResident(Resident resident) {
residents.remove(resident);
}
}
class Resident {
private String name;
private String address;
public Resident(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
```
这个程序定义了一个名为 `Community` 的类,用来表示一个小区。这个类有两个属性:小区的名称和小区的居民列表。小区的居民是由 `Resident` 类表示的,其中包含居民的名字和住址。
你可以通过调用 `Community` 类的 `addResident` 方法来添加居民,并调用 `removeResident` 方法来删除居民。你也可以通过调用 `getResidents` 方法来获取小区的居民列表。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)