我的 toString() 方法没有覆盖 Calendar toString() 方法

发布于 2024-12-14 07:18:50 字数 1357 浏览 2 评论 0原文

这是我的代码,确实非常简单。这不是作业,我正在通过一本教育书自学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 技术交流群。

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

发布评论

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

评论(1

九八野马 2024-12-21 07:18:50

这是因为您没有使用您的类,而是使用 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 ();

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