无法覆盖 toString 方法
Failed to override toString method
我写了一个定时器class。我想覆盖它的 toString
方法。但是当我调用 toString
方法时,它仍然是 returns 超级实现。 (class 的完全限定名称)
这是我的计时器 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);
}
}
我可以看到文本视图的文本是 Timer
class 的完全限定名称。我什至试过 this.toString
但它也不起作用。
您正在从您的匿名内部 class - new Runnable() { ... }
呼叫 toString()
。这意味着您在 toString()
上 上调用您的匿名 class 实例,而不是在 Timer
实例上。我怀疑你在输出中得到一个 </code>,表明它是一个匿名内部 class.</p>
<p>尝试:</p>
<pre><code>text.setText(Timer.this.toString());
... 这样您就可以在封闭的 Timer
实例上调用它。
这里有一个简短但完整的控制台应用程序来展示差异:
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()";
}
}
输出:
Test@15db9742
Test.toString()
我写了一个定时器class。我想覆盖它的 toString
方法。但是当我调用 toString
方法时,它仍然是 returns 超级实现。 (class 的完全限定名称)
这是我的计时器 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);
}
}
我可以看到文本视图的文本是 Timer
class 的完全限定名称。我什至试过 this.toString
但它也不起作用。
您正在从您的匿名内部 class - new Runnable() { ... }
呼叫 toString()
。这意味着您在 toString()
上 上调用您的匿名 class 实例,而不是在 Timer
实例上。我怀疑你在输出中得到一个 </code>,表明它是一个匿名内部 class.</p>
<p>尝试:</p>
<pre><code>text.setText(Timer.this.toString());
... 这样您就可以在封闭的 Timer
实例上调用它。
这里有一个简短但完整的控制台应用程序来展示差异:
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()";
}
}
输出:
Test@15db9742
Test.toString()