2015-08-14 7 views
5

Ho scritto una classe del timer. E voglio sovrascrivere il suo metodo toString. Ma quando chiamo il metodo toString, restituisce comunque la super implementazione. (Nome completo della classe)Impossibile eseguire l'override del metodo toString

Ecco la mia classe Timer:

import android.os.Handler; 
import android.widget.TextView; 

public class Timer implements Comparable<Timer> { 
    private Handler handler; 
    private boolean paused; 
    private TextView text; 

    private int minutes; 
    private int seconds; 

    private final Runnable timerTask = new Runnable() { 
     @Override 
     public void run() { 
      if (!paused) { 
       seconds++; 
       if (seconds >= 60) { 
        seconds = 0; 
        minutes++; 
       } 

       text.setText (toString()); //Here I call the toString 
       Timer.this.handler.postDelayed (this, 1000); 
      } 
     } 
    }; 

    //Here is the toString method, anything wrong? 
    @Override 
    public String toString() { 
     if (Integer.toString (seconds).length() == 1) { 
      return minutes + ":0" + seconds; 
     } else { 
      return minutes + ":" + seconds; 
     } 
    } 

    public void startTimer() { 
     paused = false; 
     handler.postDelayed (timerTask, 1000); 
    } 

    public void stopTimer() { 
     paused = true; 
    } 

    public void resetTimer() { 
     stopTimer(); 
     minutes = 0; 
     seconds = 0; 
     text.setText (toString()); //Here is another call 
    } 

    public Timer (TextView text) { 
     this.text = text; 
     handler = new Handler(); 
    } 

    @Override 
    public int compareTo(Timer another) { 
     int compareMinutes = ((Integer)minutes).compareTo (another.minutes); 
     if (compareMinutes != 0) { 
      return compareMinutes; 
     } 
     return ((Integer)seconds).compareTo (another.seconds); 
    } 
} 

Vedo che il testo della visualizzazione del testo è il nome completo della classe Timer. Ho anche provato this.toString ma non funziona neanche.

+0

stampata in base al valore dello schermo "Integer.toString (secondi) .length". quindi "Log.i (" value "," "+ Integer.toString (seconds) .length);" ?? –

risposta

13

Stai chiamando toString() dalla tua classe interiore anonima: lo new Runnable() { ... }. Ciò significa che stai chiamando toString()su istanza di classe anonima, non sull'istanza Timer. Sospetto che tu stia ottenendo un $1 nell'output, a dimostrazione che si tratta di una classe interna anonima.

Prova:

text.setText(Timer.this.toString()); 

... in modo che si chiama sul allegando Timer istanza, invece.

Ecco una breve ma completa console app per dimostrare la differenza:

class Test 
{ 
    public Test() { 
     Runnable r = new Runnable() { 
      @Override public void run() { 
       System.out.println(toString()); // toString on anonymous class 
       System.out.println(Test.this.toString()); // toString on Test 
      } 
     }; 
     r.run(); 
    } 

    public static void main(String[] args) { 
     new Test(); 
    } 

    @Override public String toString() { 
     return "Test.toString()"; 
    } 
} 

uscita:

[email protected] 
Test.toString() 
+2

Oh si! Grazie mille! btw sto leggendo il tuo libro! – Sweeper