案例分析一:
编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息。
class Address{
private String country;
private String province;
private String city;
private String street;
private int zipcode;
public Address() {} //无参构造方法 必须有
public Address(String country,String province,String city,String street,int zipcode) {
this.country = country;
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getInfo() {
return "国家:" + this.country + "、省份:"+ this.province + "、城市:" + this.city + "、街道:" + this.street + "、邮编:" + this.zipcode;
}
public void setCountry(String country) {
this.country = country;
}
public void setProvince(String province) {
this.province = province;
}
public void setCity(String city) {
this.city = city;
}
public void setStreet(String street) {
this.street = street;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public String getCountry() {
return this.country;
}
public String getProvince() {
return this.province;
}
public String getCity() {
return this.city;
}
public String getStreet() {
return this.street;
}
public int getZipcode() {
return this.zipcode;
}
}
public class JavaDemo{
public static void main(String args[]){
System.out.println(new Address("中华人民共和国","北京","北京","朝阳路",100000).getInfo());
}
}
网友评论