实现超级构造函数不能应用于给定的类型

时间:2016-09-16 04:42:18

标签: java

我的 IncDate 类扩展了我的 DaysBetween 类。我正在尝试在 IncDate 类中创建一个增量方法来表示第二天。但是我在我实现 super 的地方遇到此错误,我找不到修复程序。错误告诉我:

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 类中传递时不需要参数。我知道扩展类必须通过自己的构造函数。但这就是我想用 super 做的事情。那么为什么我会收到错误?我的 DaysBetween 类可以正常运行,没有任何错误,但显然其中的某些内容正在影响我的 IncDate 类。那么在我的 DaysBetween 类中忽略了哪些影响其扩展的 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 Class:

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;
            }
        }
    }
}

我的程序中有很多错误,但我觉得这个错误是零错误。请帮忙。

1 个答案:

答案 0 :(得分:0)

DaysBetween没有指定构造函数,因此这意味着使用了不带参数的默认构造函数。因为您传递参数,它会报告参数不匹配的错误。 DateClass的构造函数有三个整数,大概是你试图扩展它:

public class IncDate extends DateClass {
    ...
}

这里还有一些要记住的事情:

  • 每个文件一个公共类
  • 构造函数没有返回类型说明符
  • 确保您正在扩展正确的课程
  • 您无法在公共课程中使用公共课程