62,620
社区成员
发帖
与我相关
我的任务
分享
public interface Contents {
int value();
}
public interface Destination {
String readLabel();
}
public class Goods1 {
public Destination dest(String s) {
class GDestination implements Destination {
private String label;
private GDestination(String whereTo) {
label = whereTo;
}
public String readLabel() {
return label;
}
}
return new GDestination(s);
}
public static void main(String[] args) {
Goods1 g = new Goods1();
Destination d = g.dest("Beijing");
System.out.println(d);
}
}
C++程序员转java的通病!
多了解下java的垃圾收集器的相关知识!
class GDestination//此类在Goods1内部
implements Destination
{
private String label;
public GDestination(String whereTo)
{
label = whereTo;
}
public String readLabel()
{
return label;
}
}
public void dest2(Destination s)
{
Goods1 g = new Goods1();
//为null,则初始化
if (s == null)
{
s = g.new GDestination("aaaa");
}
}
public static void main(String[] args)
{
/*Goods1 g = new Goods1();
Destination d = g.dest("Beijing");
System.out.println(d);*/
Goods1 g = new Goods1();
Destination d = null;
g.dest2(d);
System.out.println(d);//为null,作用域体现出来了
}