2015-08-14 5 views
5

को ओवरराइड करने में विफल मैंने एक टाइमर क्लास लिखा था। और मैं अपनी toString विधि को ओवरराइड करना चाहता हूं। लेकिन जब मैं toString विधि को कॉल करता हूं, तो यह अभी भी सुपर कार्यान्वयन देता है।टूस्ट्रिंग विधि

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 वर्ग की पूरी तरह से योग्य नाम है: (वर्ग की पूरी तरह से योग्य नाम)

यहाँ मेरी घड़ी वर्ग है। मैंने this.toString भी कोशिश की लेकिन यह या तो काम नहीं करता है।

+0

आप स्क्रीन मूल्य द्वारा मुद्रित "Integer.toString (सेकंड) .length"। इस प्रकार "Log.i (" मान "," "+ Integer.toString (सेकंड)। लम्बाई);" ?? –

उत्तर

13

आप अपने अज्ञात आंतरिक वर्ग से toString() पर कॉल कर रहे हैं - new Runnable() { ... }। इसका मतलब है कि आप toString()पर पर अपने अज्ञात वर्ग उदाहरण पर कॉल कर रहे हैं, Timer उदाहरण पर नहीं। मुझे संदेह है कि आउटपुट में आपको $1 मिल रहा है, यह दर्शाता है कि यह एक अनाम आंतरिक कक्षा है।

प्रयास करें:

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

... ताकि आप के बजाय enclosing 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()"; 
    } 
} 

आउटपुट::

यहाँ अंतर प्रदर्शित करने के लिए एक छोटा लेकिन पूरा सांत्वना अनुप्रयोग है

[email protected] 
Test.toString() 
+2

ओह हाँ! आपका बहुत बहुत धन्यवाद! बीटीडब्ल्यू मैं आपकी किताब पढ़ रहा हूँ! – Sweeper

संबंधित मुद्दे