如何动态覆盖对象中的方法

时间:2013-10-26 10:30:26

标签: java override

如果这是可能的,我怎么能改变一个方法在我创建该类的实例之后所做的事情,并希望保留对该对象的引用,但覆盖它的类中的公共方法'定义?

  

这是我的代码:

    package time_applet;

    public class TimerGroup implements Runnable{

        private Timer hour, min, sec;
        private Thread hourThread, minThread, secThread;

        public TimerGroup(){
            hour = new HourTimer();
            min = new MinuteTimer();
            sec = new SecondTimer();
        }


        public void run(){
            hourThread.start();
            minThread.start();
            secThread.start();
        }

/*Please pay close attention to this method*/
        private Timer activateHourTimer(int start_time){
            hour = new HourTimer(start_time){

                public void run(){
                    while (true){

                        if(min.changed)//min.getTime() == 0)
                            changeTime();

                    }

                }
            };
            hourThread = new Thread(hour);
            return hour;
        }

        private Timer activateMinuteTimer(int start_time){

            min = new MinuteTimer(start_time){
                public void run(){
                    while (true){

                        if(sec.changed)//sec.getTime() == 0)
                            changeTime();

                    }

                }
            };
            minThread = new Thread(min);    
            return min;
        }

        private Timer activateSecondTimer(int start_time){
            sec = new SecondTimer(start_time);
            secThread = new Thread(sec);
            return sec;
        }

        public Timer addTimer(Timer timer){
            if (timer instanceof HourTimer){
                hour = timer;
                return activateHourTimer(timer.getTime());
            }
            else if (timer instanceof MinuteTimer){
                min = timer;
                return activateMinuteTimer(timer.getTime());
            }
            else{
                sec = timer;
                return activateSecondTimer(timer.getTime());        
            }
        }
    }

因此,例如在activateHourTimer()方法中,我想覆盖小时对象的run()方法,而不必创建新对象。我该怎么做?

2 个答案:

答案 0 :(得分:0)

不,你不能在同一个类中多次覆盖同一个方法而你正在创建一个匿名的HourTimer类。

      hour = new HourTimer(start_time){

            public void run(){
                while (true){

                    if(min.changed)//min.getTime() == 0)
                        changeTime();

                }

            }
        };

你可以通过在activateHourTimer方法中将if else条件作为fallows-

以另一种方式来实现
     private Timer activateHourTimer(int start_time){
       if(Condition1){
           hour = new HourTimer(start_time){

              public void run(){
                  while (true){

                      //First Condition
                  }

              }
          };
       }else if(Condition2){
           hour = new HourTimer(start_time){

              public void run(){
                  while (true){

                      //Second Condition
                  }

              }
          };
       }else{
           hour = new HourTimer(start_time){

              public void run(){
                  while (true){

                      //Third Condition
                  }

              }
          };
       }
        hourThread = new Thread(hour);
        return hour;
    }

答案 1 :(得分:0)

您不能覆盖本地(匿名)类的方法,但可以通过匿名类覆盖整个方法:

TimerGroup customTimerGroup = new TimerGroup() {
    private Timer activateHourTimer(int start_time) {
        hour = new HourTimer(start_time){ 
            public void run() {
                // do something different
            }
        };
        hourThread = new Thread(hour);
        return hour;
    }
};