我的 toString() 方法没有覆盖 Calendar toString() 方法
这是我的代码,确实非常简单。这不是作业,我正在通过一本教育书自学java:
import java.util.*;
/** @author Chris */
public class Exercise9_9 extends GregorianCalendar
{
public static void main(String[] args)
{
String[] stringList = {"s", "c", "b", "f", "e", "q", "g", "w", "i", "o"};
Integer[] integerList = {5, 7, 8, 9, 6, 5, 4, 1, 2, 3};
Calendar[] calendarList = new Calendar[10];
for (int a = 0; a < calendarList.length; a++)
{
calendarList[a] = new GregorianCalendar();
calendarList[a].set(Calendar.YEAR, ((int)Math.random()* 1000));
}
System.out.println("Largest String: " + max(stringList));
System.out.println("Largest int: " + max(integerList));
**System.out.println("Largeset date: " + (max(calendarList)).toString());**
}
public static Object max(Object[] a)
{
Arrays.sort(a);
return a[a.length-1];
}
**@Override
public String toString()**
{
return "Test";
}
}
问题是生成三个数组:int、String 和 Calendar 类型。然后从每个类别中选出最大的一个(并显示答案)。
该类扩展了 GregorianCalendar 类,这意味着我可以访问我试图覆盖的日历 toString() 。然而它不起作用。它就像 toString()
方法没有覆盖,因为我得到了默认的 toString()
输出。但是,我使用的是 Netbeans,它会确认覆盖,甚至在我单击覆盖链接时将我带到 Calendar.toString()
。所以我被困住了,任何帮助将不胜感激。
Here is my code, pretty simple really. Its not homework, I'm teaching myself java through an education book:
import java.util.*;
/** @author Chris */
public class Exercise9_9 extends GregorianCalendar
{
public static void main(String[] args)
{
String[] stringList = {"s", "c", "b", "f", "e", "q", "g", "w", "i", "o"};
Integer[] integerList = {5, 7, 8, 9, 6, 5, 4, 1, 2, 3};
Calendar[] calendarList = new Calendar[10];
for (int a = 0; a < calendarList.length; a++)
{
calendarList[a] = new GregorianCalendar();
calendarList[a].set(Calendar.YEAR, ((int)Math.random()* 1000));
}
System.out.println("Largest String: " + max(stringList));
System.out.println("Largest int: " + max(integerList));
**System.out.println("Largeset date: " + (max(calendarList)).toString());**
}
public static Object max(Object[] a)
{
Arrays.sort(a);
return a[a.length-1];
}
**@Override
public String toString()**
{
return "Test";
}
}
The question is to produce three arrays: int, String and Calendar type. Then pick out the biggest out of each category (and display the answers).
This class extends the GregorianCalendar
class which means I have access to the Calendars toString()
which I'm trying to override. However it doesn't work. Its like the toString()
method isn't overriding because I'm getting the default toString()
output. However, I'm using Netbeans and it acknowledges the override and even takes me to to Calendar.toString()
when I click the override link. So I'm stuck, any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您没有使用您的类,而是使用
GregorianCalendar
:calendarList[a] = new GregorianCalendar();
将其更改为
calendarList[a] = newexercise9_9 ();
It's because you are not using your class, but
GregorianCalendar
:calendarList[a] = new GregorianCalendar();
change this to
calendarList[a] = new Exercise9_9 ();