用Java设计实现地址概念的类Address。Address具有属性省份,市,街道,门牌号,邮编,具有能设置和获取属性的方法
时间: 2023-12-31 07:06:14 浏览: 152
可以按照以下代码实现Address类:
```java
public class Address {
private String province;
private String city;
private String street;
private String doorNumber;
private String zipCode;
public Address(String province, String city, String street, String doorNumber, String zipCode) {
this.province = province;
this.city = city;
this.street = street;
this.doorNumber = doorNumber;
this.zipCode = zipCode;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getDoorNumber() {
return doorNumber;
}
public void setDoorNumber(String doorNumber) {
this.doorNumber = doorNumber;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
```
在主函数中可以这样使用Address类:
```java
public class Main {
public static void main(String[] args) {
Address address = new Address("广东省", "深圳市", "南山区", "科技园路", "518057");
System.out.println("省份:" + address.getProvince());
System.out.println("城市:" + address.getCity());
System.out.println("街道:" + address.getStreet());
System.out.println("门牌号:" + address.getDoorNumber());
System.out.println("邮编:" + address.getZipCode());
}
}
```
输出结果为:
```
省份:广东省
城市:深圳市
街道:南山区
门牌号:科技园路
邮编:518057
```
阅读全文