日期:2014-05-20 浏览次数:20742 次
public class T { public final String name; public final String food; public final String sound; public T(String name, String food, String sound) { this.name = name; this.food = food; this.sound = sound; } public void eat() { System.out.println(name + ": Mmmmm, " + food); } public void play() { System.out.println(name + ": " + sound + " " + sound); } public void sleep() { System.out.println(name + ": Zzzzzzz..."); } public void live() { new Thread() { @Override public void run() { while (true) { eat(); play(); // sleep(); // 如果用这个代替下面的语句,会如何? T.this.sleep(); } } }.start(); } public static void main(String[] args) { new T("Fido", "beef", "Woof").live(); } }
public class T { public final String name; public final String food; public final String sound; public T(String name, String food, String sound) { this.name = name; this.food = food; this.sound = sound; } public void eat() { System.out.println(name + ": Mmmmm, " + food); } public void play() { System.out.println(name + ": " + sound + " " + sound); } public void sleep() { System.out.println(name + ": Zzzzzzz..."); } public void live() { new Thread() { public void run() { while (true) { eat(); play(); sleep(); //如果用这个代替下面的语句,会如何? //T.this.sleep(); } } }.start(); } public static void main(String[] args){ new T("Fido", "beef", "Woof").live(); } }
------解决方案--------------------