通过反射得到得到了 TalkingClock类中是有个静态方法,并且返回值是私有域的beep。在核心技术卷一中说到有方法可以借助这个方法得到私有域,求解?附上类的代码
public class TalkingClock { private int interval; private boolean beep; /** * Constructs a talking clock * @param interval the interval between messages (in milliseconds) * @param beep true if te clock should beep */ public TalkingClock(int interval,boolean beep){ this.interval = interval; this.beep = beep; } /** * Stars the clock */ public void start(){ ActionListener listener = this.new TimePrinter(); Timer t = new Timer(interval,listener); t.start(); } public class TimePrinter implements ActionListener{ // TimerPrinter is an InnerClass @Override public void actionPerformed(ActionEvent e) { Date now = new Date(); System.out.println("At the tone , the time is "+ now); if(TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep(); } } }
相关分类