(1)编写一个类Parcel1,里面有两个内部类Content和Destination。为Parcel1写一个方法void ship(String destination),这个方法调用内部类Content和Destination完成功能。 (2)编写一个类Parcel2,里面有两个内部类Content和Destination。为Parcel2写一个方法Destination to(String s)和Contents cont(),分别返回包裹的目的地和内容。
时间: 2024-02-22 18:58:45 浏览: 127
hyperapp-parcel-app:一个简单的应用程序,用于显示Hyperapp和Parcel的功能
好的,我会根据您的要求进行回答,下面是我的回答:
(1) 代码实现:
```
public class Parcel1 {
class Content {
private int price;
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
class Destination {
private String address;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
public void ship(String destination) {
Content content = new Content();
content.setPrice(100);
Destination d = new Destination();
d.setAddress(destination);
System.out.println("将包裹发送至:" + d.getAddress() + ",费用为:" + content.getPrice() + "元");
}
}
```
(2) 代码实现:
```
public class Parcel2 {
class Contents {
private int price;
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
class Destination {
private String address;
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
public Destination to(String s) {
Destination d = new Destination();
d.setAddress(s);
return d;
}
public Contents cont() {
Contents c = new Contents();
c.setPrice(100);
return c;
}
}
```
以上是我对您的问题的回答,希望能够帮助到您!
阅读全文