public class Parcel1 { class Contents { //内部类 private int i = 11; public int value() { return i; } } class Destination { //内部类 private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } public void ship(String dest) { Contents c = new Contents(); Destination d = new Destination(dest); System.out.println(d.readLabel()); } public static void main(String[] args) { Parcel1 p = new Parcel1(); p.ship("Tanzania"); } } public class Parcel2 { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } public Destination to(String s) { return new Destination(s); } public Contents cont() { return new Contents(); } public void ship(String dest) { Contents c = cont(); Destination d = to(dest); System.out.println(d.readLabel()); } public static void main(String[] args) { Parcel2 p = new Parcel2(); p.ship("Tanzania"); Parcel2 q = new Parcel2(); Parcel2.Contents c = q.cont(); Parcel2.Destination d =q.to("Borneo"); } }请你注释一下所有代码
时间: 2024-02-14 11:04:38 浏览: 140
Java内部类_动力节点Java学院整理
好的,以下是对两个类的完整注释:
```
//Parcel1类
public class Parcel1 {
//Contents类
class Contents { //内部类
private int i = 11;
public int value() {
return i;
}
}
//Destination类
class Destination { //内部类
private String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
//ship方法
public void ship(String dest) {
Contents c = new Contents();
Destination d = new Destination(dest);
System.out.println(d.readLabel());
}
//main方法
public static void main(String[] args) {
Parcel1 p = new Parcel1();
p.ship("Tanzania");
}
}
//Parcel2类
public class Parcel2 {
//Contents类
class Contents {
private int i = 11;
public int value() {
return i;
}
}
//Destination类
class Destination {
private String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() {
return label;
}
}
//ship方法
public void ship(String dest) {
Contents c = cont();
Destination d = to(dest);
System.out.println(d.readLabel());
}
//to方法,返回Destination对象
public Destination to(String s) {
return new Destination(s);
}
//cont方法,返回Contents对象
public Contents cont() {
return new Contents();
}
//main方法
public static void main(String[] args) {
Parcel2 p = new Parcel2();
p.ship("Tanzania");
Parcel2 q = new Parcel2();
Parcel2.Contents c = q.cont();
Parcel2.Destination d = q.to("Borneo");
}
}
```
希望这些注释能够对您有所帮助!
阅读全文