Дайте человеку волокно sh, и вы накормите его на день. Научите мужчину Fi sh, и вы накормите его на всю жизнь.
(Конечно, это касается и женщин.)
Я пытаюсь научить вас Fi sh. Я объясняю, как отловить такую ошибку, не спрашивая сначала о переполнении стека.
Настройте свою среду IDE, чтобы она предупреждала вас, когда вы используете объект (экземпляр) для вызова метода stati c. То есть всякий раз, когда вы делаете что-то вроде
yourObject.someStaticMethod();
В моем Eclipse ваш код выдает это предупреждение:
Метод stati c ofDays (int) из типа Period должен быть доступ к нему осуществляется путем c
Это говорит нам, что ofDays()
является методом c stati, и поэтому результат вызова такой же, как и при вызове Period.ofDays(3)
. Eclipse даже предлагает решить эту проблему для меня:
![enter image description here](https://i.stack.imgur.com/O9Bhc.png)
After I click “Change access to static using Period (declaring type)”, that code line becomes:
Period.ofYears(1).ofMonths(2);
Period period = Period.ofDays(3);
Now I think you can see why you got the result you got.
So:
- Make sure your IDE is configured to issue such warnings. It doesn’t have to be Eclipse, other IDEs can do the same.
- Read those warnings when you get them and make sure you understand them. If you've skipped a warning once, if you get an unexpected result, go back and read the warning again. If you still need to ask on Stack Overflow, you're welcome of course, and consider including the text of that warning you didn't understand.
Others have nicely explained how to fix your code to give you your desired result. If Period.of(1, 2, 3)
is a bit confusing because you can't tell what's years, months weeks and days, use Period.ofYears(1).plusMonths(2).plusDays(3)
, and everything is clear. It also resembles what you tried in the question.
Link: Как отлаживать небольшие программы с множеством советов, подобных тому, который я даю здесь (и не потому, что ваш вопрос был плохим , хороший, я проголосовал за).