setZone方法有问题吗?

问题描述 投票:0回答:1

我正在制作一个扩展时钟以包含时区名称的程序。派生的类需要具有值的静态String数组数据成员: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程序不能编译,因为它说找不到符号。我做错什么了吗?

java derived-class mutators
1个回答
0
投票

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

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

应该是:Obj.method()

yourExtClock.setZone(5,45,16,"CDT");
© www.soinside.com 2019 - 2024. All rights reserved.