Java 中的 toString 方法

发布于 2024-11-03 08:44:54 字数 588 浏览 0 评论 0原文

为什么时间显示不正常(可读)?缺少 toString 方法?

import java.util.Calendar;

abstract class Calender {

    abstract void showTime();

    void time() {
        Calendar rightNow = Calendar.getInstance();
        System.out.println("Time: " + rightNow);
    }

    public String toString() {
        return "Calender [toString()=" + super.toString() + "]";
    }

}

class Calender1 extends Calender {
    public static void main (String[] args) {
        Calender1 cal = new Calender1();
        cal.time();
    }

    void showTime() {       
    }
}

谢谢。 Java菜鸟。

Why the time is shown not in normal (readable) way? Method toString is missing?

import java.util.Calendar;

abstract class Calender {

    abstract void showTime();

    void time() {
        Calendar rightNow = Calendar.getInstance();
        System.out.println("Time: " + rightNow);
    }

    public String toString() {
        return "Calender [toString()=" + super.toString() + "]";
    }

}

class Calender1 extends Calender {
    public static void main (String[] args) {
        Calender1 cal = new Calender1();
        cal.time();
    }

    void showTime() {       
    }
}

Thank you.
Java rookie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花开雨落又逢春i 2024-11-10 08:44:54

您可以使用以下代码显示当前时间:

DateFormat timeFormat = DateFormat.getTimeInstance();
Calendar currentDate = Calendar.getInstance();
System.out.println(timeFormat.format(currentDate.getTime()));

You can display the current time with the following code:

DateFormat timeFormat = DateFormat.getTimeInstance();
Calendar currentDate = Calendar.getInstance();
System.out.println(timeFormat.format(currentDate.getTime()));
佞臣 2024-11-10 08:44:54

不太确定你实际上在这里做什么。

看起来您已经编写了自己的抽象类 Calendar 并在 Calendar1 中继承了它。

您编写的第一个 Calendar 类继承自对象,该对象的 toString 不会有趣/有用。

日历上 toString 的文档位于:
http://download.oracle.com /javase/1.4.2/docs/api/java/util/Calendar.html#toString()

底线 - 不要依赖它。

Not really sure what you're actually doing here.

It looks like you've written your own abstract class Calendar and inherited from it in Calendar1.

The first Calendar class you wrote inherits from object the toString of which will not be interesting / useful.

The docs for toString on a calendar are here:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#toString()

Bottom line - don't rely on it.

柳絮泡泡 2024-11-10 08:44:54

为什么在基类中使用 super ?我不太确定你想做什么,但在你的基类 Calendar 中你只需要 toString() 方法。

在您的 Calender1 类中,您还需要创建一个 toString() 方法。在那里你会调用 use super.toString() 这是因为你是从日历继承的所以它是你的超类

Why are you using super in your base class? I'm not really sure what you are trying to do but in you base class Calendar you just need the toString() method.

And in your Calender1 class you need to create a toString() method as well. In there you would call use super.toString() this is because you are inheriting from calendar so its your super class

二智少女 2024-11-10 08:44:54

您正在调用没有父类的 super (实际上您正在调用 Object.toString()
这将返回类的名称和内存地址:
http://download.oracle .com/javase/6/docs/api/java/lang/Object.html#toString%28%29

你可能想要这样的东西:

@Override
public String toString() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
    return "Calender [toString()=" + formatter.format(rightNow.getTime()) + "]";
}

You are calling super with no parent class (you are calling Object.toString(), actually)
This will return the name of the class and the memory address:
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29

You probably want something like this:

@Override
public String toString() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
    return "Calender [toString()=" + formatter.format(rightNow.getTime()) + "]";
}
鲸落 2024-11-10 08:44:54

您可能尝试过以下操作:

Calendar cal = Calendar.getInstance();
System.out.println(cal);

它给出这样的输出:

java.util.GregorianCalendar[time=1303645990437,areFieldsSet=true,areAllFieldsSet=true,...

这是 GregorianCalendar 的 toString 方法,它给出了其实现的所有细节状态细节,而不是可读的日期时间表示。

作为实验,您可以创建一个匿名内部类,使用不同的 toString 实现来扩展 GregorianCalendar:

Calendar cal = new GregorianCalendar() {        
    public String toString() {
        return getTime().toString();
    }
};
System.out.println(cal);

这将打印:

Sun Apr 24 13:53:10 CEST 2011

(YMMV)。

然而,这有点古怪。通常,您会使用 DateFormat (如其他几个答案中所示),将日历的表示与其实现分离。

You probably tried this:

Calendar cal = Calendar.getInstance();
System.out.println(cal);

Which gives output like this:

java.util.GregorianCalendar[time=1303645990437,areFieldsSet=true,areAllFieldsSet=true,...

This is the toString method of GregorianCalendar, which gives all the nitty gritty state details of its implementation, not a readable date-time representation.

As an experiment, you could create an anonymous inner class extending GregorianCalendar with a different toString implementation:

Calendar cal = new GregorianCalendar() {        
    public String toString() {
        return getTime().toString();
    }
};
System.out.println(cal);

This prints:

Sun Apr 24 13:53:10 CEST 2011

(YMMV).

However, this is a bit kinky. Normally you would use a DateFormat as shown in several other answers, to decouple the presentation of your Calendar from its implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文