简体   繁体   中英

Failed to override toString method

I wrote a timer class. And I want to override its toString method. But when I call the toString method, it still returns the super implementation. (fully qualified name of the class)

Here is my timer class:

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);
    }
}

I can see that the text view's text is the fully qualified name of the Timer class. I even tried this.toString but it doesn't work either.

You're calling toString() from your anonymous inner class - the new Runnable() { ... } . That means you're calling toString() on your anonymous class instance, not on the Timer instance. I suspect you're getting a $1 in the output, showing that it's an anonymous inner class.

Try:

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

... so that you call it on the enclosing Timer instance instead.

Here's a short but complete console app to demonstrate the difference:

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()";
    }
}

Output:

Test$1@15db9742
Test.toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM