定义一个Address地址类,为Address派生出InternationalAddress国际地址类和DomesticAddress国内地址类,在Address类中定义display()方法显示Address类的各属性。
protected String postalcode; //邮政编码
protected String state; //国家
protected String province; //省
protected String city; //城市
protected String street; //街道
两个子类分别定义display()方法,显示两种不同格式的地址全名
国内地址的格式为:邮政编码、国家、 省、城市、街道名,
国外地址的格式为:街道名、城市、省、国家、邮政编码。
国际地址格式:
1515 Broadway,New York,Washington,USA,10036
国内地址格式:
410024,中国湖南省长沙市芙蓉路888号
两个子类分别实现国内国外地址输出
回答:还是很简单的,推荐学习书籍
链接:https://pan.baidu.com/s/1c9aSL4LiA-zGTcMhShVNgg
提取码:0925
public class Address {
protected String postalcode; //邮政编码
protected String state; //国家
protected String province; //省
protected String city; //城市
protected String street; //街道
public Address(String postalcode, String state, String province, String city, String street) {
this.postalcode = postalcode;
this.state = state;
this.province = province;
this.city = city;
this.street = street;
}
protected void display() {
}
}
public class InternationalAddress extends Address {
public InternationalAddress(String postalcode, String state, String province, String city, String street) {
super(postalcode, state, province, city, street);
}
// 国外地址的格式为:街道名、城市、省、国家、邮政编码。
@Override
protected void display() {
System.out.println(street + city + province + state + postalcode);
}
}
public class DomesticAddress extends Address {
public DomesticAddress(String postalcode, String state, String province, String city, String street) {
super(postalcode, state, province, city, street);
}
// 国内地址的格式为:邮政编码、国家、 省、城市、街道名,
@Override
protected void display() {
System.out.println(postalcode + state + province + city + street);
}
}
public class TestDemo {
public static void main(String[] args) {
// 自己填啦
Address address1 = new InternationalAddress("", "", "", "", "");
Address address2 = new DomesticAddress("", "", "", "", "");
address1.display();
address2.display();
}
}