【问题标题】:Is there something wrong with the setZone method?setZone 方法有问题吗?
【发布时间】:2020-06-24 22:53:46
【问题描述】:

我正在制作一个扩展时钟以显示时区名称的程序。派生类需要有一个静态字符串数组数据成员,其值:EST、CST、MST、PST、EDT、CDT、MDT、PDT、一个区域数据成员、一个默认构造函数、一个带参数的构造函数、setZone() 方法、getZone() 方法、printTime() 方法、toString()、equals() 方法、makeCopy() 方法和 getCopy() 方法。

public class Clock {
    private int hr;
    private int min;
    private int sec;
    public Clock() {
        hr = 0;
        min = 0;
        sec = 0;
    }
    public Clock(int hours, int minutes, int seconds) {
        if (0 <= hours && hours < 24) {
            hr = hours;
        }
        else {
            hr = 0;
        }
        if (0 <= minutes && minutes < 60) {
            min = minutes;
        }
        else {
            min = 0;
        }
        if (0 <= seconds && seconds < 60) {
            sec = seconds;
        }
        else {
            sec = 0;
        }
    }
    public Clock(Clock otherClock) {
        hr = otherClock.hr;
        min = otherClock.min;
        sec = otherClock.sec;
    }
    public void setTime(int hours, int minutes, int seconds) {
        if (0 <= hours && hours < 24) {
            hr = hours;
        }
        else {
            hr = 0;
        }
        if (0 <= minutes && minutes < 60) {
            min = minutes;
        }
        else {
            min = 0;
        }
        if (0 <= seconds && seconds < 60) {
            sec = seconds;
        }
        else {
            sec = 0;
        }
    }
    public int getHours() {
        return hr;
    }
    public int getMinutes() {
        return min;
    }
    public int getSeconds() {
        return sec;
    }
    public void printTime() {
        if (hr < 10) {
            System.out.print("0");
        }
        System.out.print(hr + ":");

        if (min < 10) {
            System.out.print("0");
        }
        System.out.print(min + ":");

        if (sec < 10) {
            System.out.print("0");
        }
        System.out.print(sec);
    }
    public void incrementHours() {
        hr++;
        if (hr > 23) {
            hr = 0;
        }
    }
    public void incrementMinutes() {
        min++;
        if (min > 59) {
            min = 0;
            incrementHours();
        }
    }
    public void incrementSeconds() {
        sec++;
        if (sec > 59) {
            sec = 0;
            incrementMinutes();
        }
    }
    public boolean equals(Clock otherClock) {
        return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
    }
    public void makeCopy(Clock otherClock) {
        hr = otherClock.hr;
        min = otherClock.min;
        sec = otherClock.sec;
    }
    public Clock getCopy() {
        Clock temp = new Clock();
        temp.hr = hr;
        temp.min = min;
        temp.sec = sec;
        return temp;
    }
    public String toString() {
        String str = "";
        if (hr < 10) {
            str = "0";
        }
        str += hr + ":";

        if (min < 10) {
            str += "0";
        }
        str += min + ":";

        if (sec < 10) {
            str += "0";
        }
        str += sec;

        return str;
    }
}

class ExtClock extends Clock {
    static String[] timeZone = {"EST", "CST", "MST", "PST", "EDT", "CDT", "MDT", "PDT"};
    private String zone;
    public ExtClock() {
        super();
        zone = "";
    }
    public ExtClock(int hours, int minutes, int seconds, String tz) {
        super(hours, minutes, seconds);
        zone = tz;
    }
    public void setZone(int hours, int minutes, int seconds, String tz) {
        setTime(hours, minutes, seconds);
        zone = tz;
    }
    public String getZone() {
        return zone;
    }
    public void printTime() {
        super.printTime();
        System.out.println(" " + zone);
    }
    public String toString() {
        return super.toString() + " " + zone;
    }
    public boolean equals(ExtClock otherClock) {
        return super.equals(otherClock) && zone.equalsIgnoreCase(otherClock.zone);
    }
}

public class ExtClockTest {
    public static void main(String[] args) {
        ExtClock myExtClock = new ExtClock(5,4,30,"EST");
        ExtClock yourExtClock = new ExtClock(0,0,0,"");
        setZone.yourExtClock(5,45,16,"CDT");
    }
}

派生类可以正常编译,但 ExtClockTest 程序无法编译,因为它说找不到符号。我做错了吗?

【问题讨论】:

  • 找不到符号______......重要的是后面的内容......
  • 你的意思是yourExtClock.setZone?另外,你似乎在重塑OffsetTime
  • 将 setZone.yourExtClock(5,45,16,"CDT") 替换为 yourExtClock.setZone(5,45,16,"CDT");
  • 尝试使用 Intellij IDEA 或 Eclipse 之类的 IDE,它会突出显示错误并使编程更简单

标签: java derived-class mutators


【解决方案1】:

您已将方法放在对象之前。

setZone.yourExtClock(5,45,16,"CDT");

应该是: obj.method()

yourExtClock.setZone(5,45,16,"CDT");

【讨论】:

  • 感谢您的信息!
  • 请点击对您最有帮助的答案左侧的复选标记来关闭您的问题@Superpan21
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 2015-08-28
  • 2012-04-08
  • 2012-07-08
相关资源
最近更新 更多