2016-09-16 33 views
-3

我的IncDate類擴展我的天DaysBetween類。我試圖在我的IncDate類中創建一個增量方法來表示第二天。然而,我得到這個錯誤就在我實施超級旁邊,我找不到修復程序。該錯誤是告訴我:實施超級構造函數不能應用於給定類型

Constructor DaysBetween in class DaysBetween cannot be applied to given types; 
required:no arguments 
found:int,int,int 
reason:actual and formal argument lists differ in length 

爲什麼錯誤,說不需要爭論時,他們實際上我DaysBetween類中通過。我知道擴展類必須通過他們自己的構造函數。但這正是我想用超級。那麼,爲什麼我會得到這個錯誤?我的天之間類的工作,因爲它應該沒有任何錯誤,但很明顯,其中的東西正在影響我的IncDate類。那麼在我的天內有什麼可忽略的類正在影響其擴展IncDate類?

DaysBetween類:

package dateclass1; 
import java.text.DateFormatSymbols; 
import java.util.Calendar; 
import java.util.Scanner; 

class DateClass { 
protected int year; 
protected int month; 
protected int day; 
public static final int MINYEAR = 1583; 
// Constructor 
public DateClass(int newMonth, int newDay, int newYear) 
{ 
month = newMonth; 
day = newDay; 
year = newYear; 
} 
// Observers 
public int getYear() 
{ 
return year; 
} 
public int getMonth() 
{ 
return month; 
} 
public int getDay() 
{ 
return day; 
} 
public int lilian() 
{ 
// Returns the Lilian Day Number of this date. 
// Precondition: This Date is a valid date after 10/14/1582. 
// 
// Computes the number of days between 1/1/0 and this date as if no calendar 
// reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1. 

final int subDays = 578100; // number of calculated days from 1/1/0 to 10/14/1582 November 17, 1858 
int numDays; 
// Add days in years. 
numDays = year * 365; 
// Add days in the months. 
if (month <= 2) 
numDays = numDays + (month - 1) * 31; 
else 
numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27)/10); 
// Add days in the days. 
numDays = numDays + day; 
// Take care of leap years. 
numDays = numDays + (year/4) - (year/100) + (year/400); 
// Handle special case of leap year but not yet leap day. 
if (month < 3) 
{ 
if ((year % 4) == 0) numDays = numDays - 1; 
if ((year % 100) == 0) numDays = numDays + 1; 
if ((year % 400) == 0) numDays = numDays - 1; 
} 
// Subtract extra days up to 10/14/1582. 
numDays = numDays - subDays; 
return numDays; 
} 
@Override 
public String toString() 
// Returns this date as a String. 
{ 
String monthString = new DateFormatSymbols().getMonths()[month-1]; 
return(monthString + "/" + day + "/" + year); 
} 
public class mjd 
{ 
    public int mjd() 
    { 
    final int subDays = 678941; 
    int numDays; 
    numDays = year * 365; 
    if (month <= 2) 
     numDays = numDays + (month - 1) * 31; 
    else 
     numDays = numDays + ((month -1) * 31) - ((4 * (month-1) + 27)/10); 
    numDays = numDays + day; 
    numDays = numDays + (year/4) - (year/100) + (year/400); 
    if (month < 3) 
    { 
     if ((year % 4) == 0) numDays = numDays -1; 
     if ((year % 100) == 0) numDays = numDays + 1; 
     if ((year % 400) == 0) numDays -= numDays -1; 
    } 
    // Days subtracted up to 10/14/1582 
    numDays = numDays - subDays; 
    return numDays; 
    } 
} 

public class djd 
{ 
    public int djd() 
    { 
    final int subDays = 693961; // number of calculated days from 1/1/0 to January 1,1900 
    int numDays; 
// Add days in years. 
    numDays = year * 365; 
// Add days in the months. 
    if (month <= 2) 
     numDays = numDays + (month - 1) * 31; 
    else 
     numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27)/10); 
// Add days in the days. 
    numDays = numDays + day; 
// Take care of leap years. 
    numDays = numDays + (year/4) - (year/100) + (year/400); 
// Handle special case of leap year but not yet leap day. 
    if (month < 3) 
    { 
     if ((year % 4) == 0) numDays = numDays - 1; 
     if ((year % 100) == 0) numDays = numDays + 1; 
     if ((year % 400) == 0) numDays = numDays - 1; 
    } 
// Subtract extra days up to 10/14/1582. 
    numDays = numDays - subDays; 
    return numDays; 
    } 
} 

} 
public class DaysBetween 
{ 
    public static void main(String[] args) 
    { 
    Scanner conIn = new Scanner(System.in); 
    int day, month, year; 

    System.out.println("Enter two 'modern' dates: month day year"); 
    System.out.println("For example, January 12, 1954, would be: 1 12 1954"); 
    System.out.println(); 
    System.out.println("Modern dates occur after " + DateClass.MINYEAR + "."); 
    System.out.println(); 

    System.out.println("Enter the first date:"); 
    month = conIn.nextInt(); 
    day = conIn.nextInt(); 
    year = conIn.nextInt(); 
    DateClass date1 = new DateClass(month, day, year); 

    System.out.println("Enter the second date:"); 
    month = conIn.nextInt(); 
    day = conIn.nextInt(); 
    year = conIn.nextInt(); 
    DateClass date2 = new DateClass(month, day, year); 

    if ((date1.getYear() <= DateClass.MINYEAR) 
     || 
     (date2.getYear() <= DateClass.MINYEAR)) 
     System.out.println("You entered a 'pre-modern' date."); 
    else 
    { 
     System.out.println("The number of days between"); 
     System.out.print(date1); 
     System.out.print(" and "); 
     System.out.print(date2); 
     System.out.print(" is "); 
     System.out.println(Math.abs(date1.lilian() - date2.lilian())); 
    } 
    } 
} 

IncMain類:

package dateclass1; 

import java.util.Calendar; 

/** 
* 
* @author macbookpro 
*/ 
class IncDate extends DaysBetween { 
    public IncDate(int newMonth, int newDay, int newYear) 
    { 
     super(newMonth, newDay, newYear); 
    } 
    public void increment() 
    { 
    if(month == 1){ 
    if(day>30){ 
     day=1; 
     month=2; 
    } 
    else{ 
     day=day+1; 
     } 
    } 
    else if (month == 2){ 
     if ((year%4 == 0 && year%100 !=0) || year%400 == 0) 
    { 
     if(day>28){ 
      day = 1; 
      month=3; 
     } 
     else{ 
      day = day +1; 
      } 
     } 
     else{ 
     if(day > 27){ 
      day = 1; 
      month = 3; 
     } 
     else{ 
      day = day + 1; 
      } 
      } 
     } 
     else if(month == 3){ 
      if(day > 30){ 
       day=1; 
       month=4; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month == 4){ 
      if(day > 29){ 
       day = 1; 
       month = 5; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month ==5){ 
      if(day>30){ 
       day = 1; 
       month= 6; 
      } 
      else{ 
       day = day +1; 
      } 
     } 
     else if (month==6){ 
      if(day>29){ 
       day = 1; 
       month=7; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month==7){ 
      if (day>30){ 
       day = 1; 
       month = 8; 
      } 
      else{ 
       day = day +1; 
      } 
     } 
     else if (month==8){ 
      if (day>30){ 
       day = 1; 
       month = 9; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month==9){ 
      if (day>29){ 
       day = 1; 
       month = 10; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month==10){ 
      if (day>30){ 
       day = 1; 
       month = 11; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month==11){ 
      if (day>29){ 
       day = 1; 
       month = 12; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
     else if (month==12){ 
      if (day>30){ 
       day = 1; 
       month = 1; 
       year = year+1; 
      } 
      else{ 
       day = day + 1; 
      } 
     } 
    } 
} 

有很多在我的程序錯誤的,但我覺得好像這個bug是地面零。請幫忙。

+1

我看不到'DaysBetween'中有一個構造函數需要3個參數。 –

+0

你正在擴大錯誤的班級。 'IncDate'應該擴展'DateClass',而不是'DaysBetween'(這只是一個主要方法) –

+0

好的,謝謝謝謝謝謝謝謝 –

回答

0

DaysBetween沒有指定構造函數,這意味着使用不帶參數的默認構造函數。由於您傳遞參數,因此會報告參數不匹配的錯誤。 DateClass已與三個整數構造,想必你想以擴展:

public class IncDate extends DateClass { 
    ... 
} 

這裏也有一些事情要記住:

  • 每個文件包含一個公共類
  • 構造函數沒有返回類型說明符
  • 確保您正在擴展正確的類別
  • 您不能在公共類中使用公共類
+0

好的,謝謝!這就是我所需要的一些提示 –

相關問題