Java 中的 toString 方法
为什么时间显示不正常(可读)?缺少 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用以下代码显示当前时间:
You can display the current time with the following code:
不太确定你实际上在这里做什么。
看起来您已经编写了自己的抽象类 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.
为什么在基类中使用 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
您正在调用没有父类的
super
(实际上您正在调用Object.toString()
)这将返回类的名称和内存地址:
http://download.oracle .com/javase/6/docs/api/java/lang/Object.html#toString%28%29
你可能想要这样的东西:
You are calling
super
with no parent class (you are callingObject.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:
您可能尝试过以下操作:
它给出这样的输出:
这是 GregorianCalendar 的 toString 方法,它给出了其实现的所有细节状态细节,而不是可读的日期时间表示。
作为实验,您可以创建一个匿名内部类,使用不同的 toString 实现来扩展 GregorianCalendar:
这将打印:
(YMMV)。
然而,这有点古怪。通常,您会使用 DateFormat (如其他几个答案中所示),将日历的表示与其实现分离。
You probably tried this:
Which gives output like this:
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:
This prints:
(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.