如何在Java中调用另一个构造函数? (初学者的例子)

时间:2017-11-19 21:37:42

标签: java constructor

首先,我已经阅读了this个问题,我知道这个问题正在处理我遇到的同样的基本问题。

尽管如此,我无法将解决方案应用于我自己的特定问题。

在以下示例中,我有一些重载的Clock构造函数。在我尝试从字符串输入创建时钟的情况下会发生错误。

有没有人有正确实施构造函数调用的任何提示?

代码:

    public class Clock 
    {
        public static void main(String[] args) 
        {

            Clock testClock = new Clock(153, 26);
            System.out.println("Input: Clock(153, 26)");
            System.out.println(testClock.toString());

            Clock testClock2 = new Clock(9000);
            System.out.println("Input: Clock(9000)");
            System.out.println(testClock2.toString());


            Clock testClock3 = new Clock(23:59);
            System.out.println("Input: Clock(23:59)");
            System.out.println(testClock3.toString());

            System.out.println("Input: testClock2.add(20)");
            System.out.println(testClock2.add(20));             

            System.out.println("input: testClock.add(testClock2)");
            System.out.println(testClock.add(testClock2).toString());

        }

        // Constructors

        public Clock(int min, int h)
        {
            this.min += min%60;
            h += min/60;
            this.h += h%24;
        }

        public Clock(int min)
        {
            this.min += min%60;
            this.h += (min/60)%24;
        }



        public Clock (String theTime)
        {
            int minutes = Integer.parseInt(theTime.substring(0,1));
            int hours = Integer.parseInt(theTime.substring(3,4));

            Clock stringClock = new Clock(minutes, hours); 
            return stringClock; //error occurs *********************

        }



        private int h;      
        private int min; 

        public int getMin() {
        return min;
        }
        public int getH() {
        return h;
        }

        public Clock add(int min)
        {
            int newMin = this.min + min;
            int newH = this.h;

            Clock newClock = new Clock(newMin, newH);
            return newClock;
        }

        public Clock add(Clock c)
        {
            int newMin = this.min + c.min;
            int newH = this.h + c.h;

            Clock newClock = new Clock(newMin, newH);
            return newClock;
        }

        public String toString()
        {

            String theTime = "";

            if (this.h < 10)
            {
                theTime += "0" + this.h;
            }
            else
            {
                theTime += this.h;
            }

            theTime += ":";

            if (this.min < 10)
            {
                theTime += "0" + this.min;
            }
            else 
            {
                theTime += this.min;
            }

            return theTime;
        }
    }

3 个答案:

答案 0 :(得分:2)

您可以致电this但它应该是第一个声明,例如:

    public Clock (String theTime)
    {
        this(
            Integer.parseInt(theTime.substring(0,1)),
            Integer.parseInt(theTime.substring(3,4))
        );

    }

或者您可以使用静态工厂方法:

    public static Clock parseHHMM(String theTime)
    {
        int hh = Integer.parseInt(theTime.substring(0,1));
        int mm = Integer.parseInt(theTime.substring(3,4));
        return new Clock(hh, mm);

    }

我更喜欢后者,这是Java中常见的方法,例如: here

答案 1 :(得分:2)

该行还有一个问题:

Clock testClock3 = new Clock(23:59);

如果您希望将参数视为String,则应将带有引号的值作为参数括起来,如下所示:

Clock testClock3 = new Clock("23:59");

,因为当你不改变传递的参数外观时,它就不会编译。

答案 2 :(得分:0)

您可以考虑在分号theTime中拆分":",以便获得两个整数的数组,第一个是小时,第二个是分钟。看看

    public Clock (String theTime)
    {
        this(Integer.parseInt(theTime.split(":")[1],
        Integer.parseInt(theTime.split(":")[0]);
    }

希望这会有所帮助