浏览网页的时候看到介绍JAVA内部类,页面上给出如下代码,不过我没太看懂它的意思,为什么这么写?main方法里的写法也不太懂。我是初学,我会自己看一些别的书来弄懂的,不过也想听听大家是如何理解的。public interface Contents {
int value();
}
public interface Destination {
String readLabel();
}
public class Goods {
private class Content implements Contents {
private int i = 11;
public int value() {
return i;
}
}
protected class GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
public Destination dest(String s) {
return new GDestination(s);
}
public Contents cont() {
return new Content();
}
}
class TestGoods {
public static void main(String[] args) {
Goods p = new Goods();
Contents c = p.cont();
Destination d = p.dest("Beijing");
}
}
int value();
}
public interface Destination {
String readLabel();
}
public class Goods {
private class Content implements Contents {
private int i = 11;
public int value() {
return i;
}
}
protected class GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
public Destination dest(String s) {
return new GDestination(s);
}
public Contents cont() {
return new Content();
}
}
class TestGoods {
public static void main(String[] args) {
Goods p = new Goods();
Contents c = p.cont();
Destination d = p.dest("Beijing");
}
}
2、MAIN函数中 Contents c = p.cont();
Contents c = p.cont();
是调用p对象中的cont();方法
你问的是什么意思啊。。
说的明白点
return new GDestination(s);
}
public Contents cont() {
return new Content();
}以上的这段代码是什么意思?有点迷糊~
public Destination dest(String s) {
return new GDestination(s);
} //返回一个GDestination类型的对象;在你所给的程序里似乎是通过dest方法生成下一个目的地的对象public Contents cont() {
return new Content();
} //返回一个Content类型的对象
如果要是Contents接口的话。?
public Contents cont() {
return new Contents();
}
怎么可能给接口实例化??