简体   繁体   English

无法覆盖toString方法

[英]Failed to override toString method

I wrote a timer class. 我写了一个计时器类。 And I want to override its toString method. 我想覆盖它的toString方法。 But when I call the toString method, it still returns the super implementation. 但是当我调用toString方法时,它仍然返回超级实现。 (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. 我可以看到文本视图的文本是Timer类的完全限定名称。 I even tried this.toString but it doesn't work either. 我甚至尝试过this.toString但它也不起作用。

You're calling toString() from your anonymous inner class - the new Runnable() { ... } . 你从你的匿名内部类 - new Runnable() { ... }调用toString() That means you're calling toString() on your anonymous class instance, not on the Timer instance. 这意味着你在匿名类实例调用toString() ,而不是在Timer实例调用。 I suspect you're getting a $1 in the output, showing that it's an anonymous inner class. 我怀疑你输出的$1 ,这表明它是一个匿名的内部类。

Try: 尝试:

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

... so that you call it on the enclosing Timer instance instead. ...所以你可以在封闭的Timer实例上调用它。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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