2016-09-23 66 views
0

所以我通讀了幾個不同的主題,但他們都沒有直接解決我如何解決我的問題。我試圖創建一個CalendarGregorian),然後使用.complete()方法,以便在我的班級中使用此類(Paycheck),我可以從這些日期找到相關的dates並創建new Calendar(s)以確定支付的工資和欠付的工資。但是,它告訴我,.complete().computeTime().computeFields()都不可見。日曆(GregorianCalendar).complete()方法不可見?

從我讀過的,這似乎是因爲他們是受保護的方法,即使我爲他們,我不能訪問他們,因爲該類不在我的包。我如何得到這個,以便我可以撥打.complete()方法?

import java.util.Calendar; 
import java.util.GregorianCalendar; 
import java.util.TimeZone; 


public class Paycheck { 
    //fields 
    protected double grossAmount; 
    protected Calendar paymentDate; 
    protected Calendar payPeriodStart; 

    public Paycheck(double grossAmount, int iYear, int iMonth, int iDay, int sYear, int sMonth, int sDay) { 
     this.grossAmount = grossAmount; 

     TimeZone tz1 = TimeZone.getTimeZone("America/Chicago"); 
     this.paymentDate = new GregorianCalendar(iYear, iMonth, iDay); 
     this.paymentDate.setTimeZone(tz1); 
     this.paymentDate.complete(); //says "method not visible" 


     this.payPeriodStart = new GregorianCalendar(sYear, sMonth, sDay); 
     this.payPeriodStart.setTimeZone(tz1); 

    } 
+0

你爲什麼要調用一個內部方法。你怎麼知道它存在?它沒有在javadoc中描述。你真的想在這裏完成什麼?如果你不叫'complete()',你認爲你有什麼樣的問題?忘記方法存在,並做你需要做的。沒有它,'GregorianCalendar'將正常工作。 – Andreas

+0

@Andreas當我評論了其他幾個地方時,我這樣做是因爲該方法存在的事實(https://www.tutorialspoint.com/java/util/java_util_calendar.htm)讓我覺得有必要將所有其他領域,因爲它檢查,看看他們是否都設置(這使我認爲字段不會自動設置在構造函數。我的道歉。感謝您的幫助! –

+0

爲什麼你閱讀時,當java有更好的描述,並且是*官方*規範?請參閱https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html – Andreas

回答

0

衍生在comment你寫道:

我不關心的實際時間,我只是希望它給我日期,以便我可以確定每週的週日(根據各州的法律重要)。

這很容易做到,並且不需要對內部方法進行任何調用。

Calendar cal = new GregorianCalendar(2016, Calendar.SEPTEMBER, 22); 
cal.setTimeZone(TimeZone.getTimeZone("America/Chicago")); 
System.out.println("Day of week (1=Sun, ..., 7=Sat): " + cal.get(Calendar.DAY_OF_WEEK)); 

輸出

Day of week (1=Sun, ..., 7=Sat): 5 

輸出是5因爲今天是Thu 9/22/2016,就是這樣被賦予了日期。

+0

根據我對另一個答案的評論:「好吧......也許這是我錯誤的解釋方法規範...所以你說的是,在構造函數中給它一個日期設置所有的字段,如dayofweek等等?我的意思是,這對我來說很有意義,但complete()方法明確表示它設置了所有未設置的字段,這使得我不會自動計算它。這是我的錯,如果是這樣的話,我想要測試一下。「 –

+0

@BrandonHunter不正確。決定何時「設置所有字段」完全取決於內部實施。它實際上是* deferred *直到需要,這意味着在提取請求的字段之前調用'get()'將首先調用'complete()'。 – Andreas

+0

感謝您解決這個問題。目前爲止,我在編程方面的大部分經驗都在C語言中,所以我習慣於自己做所有的腳步工作。所以這是我的錯。感謝知識哈哈。 –

-1

讓我們比較打字努力:

this.paymentDate = new GregorianCalendar(iYear, iMonth, iDay); 
this.paymentDate.complete(); // Why do you need to call this one? 

VS代碼具有相同的效果

this.paymentDate = new GregorianCalendar(iYear, iMonth, iDay, 0, 0, 0); 
// constructor taking extra hours, minutes, seconds ----------^ 

那麼爲什麼你嘗試調用一個受保護的方法的幾乎是不可能的?


在某些情況下,數量非常有限,確實需要調用protected/private方法,這可能是可能的。例證:

Class<GregorianCalendar> clazz=GregorianCalendar.class; 
Method completeMethod = clazz.getDeclaredMethod("complete"); 
// this does the trick if it doesn't throw security exceptions 
completeMethod.setAccessible(true); 
completeMethod.invoke(this.paymentDate); 

見的Javadoc AccesibleObject.setAccessibleMethod正在從AccessibleObject

+0

我正在使用此結構體: 「GregorianCalendar(int year,int month,int dayOfMonth) 這構造了一個GregorianCalendar,其中給定日期在默認時區中使用默認語言環境設置。 每https://www.tutorialspoint.com/java/util/java_util_gregoriancalendar.htm –

+0

我不在乎實際的時間,我只是想讓它給我的日期,所以我可以確定每週的重要性(重要的基礎上各州法律)。 –

+0

另外,儘管我不確定你在做什麼,但我只是爲了以防萬一,我錯過了一些東西。沒有解決可見性問題。 –