gpt4 book ai didi

java - 从另一个类调用方法时遇到很多麻烦

转载 作者:行者123 更新时间:2023-12-01 13:49:35 25 4
gpt4 key购买 nike

我的问题:我不断收到错误......(实际上有多个错误......)
"类 Appointment 中的方法 getYear 不能应用于给定类型; 必需:整数 发现:没有参数 原因:实际参数列表和形式参数列表的长度不同“

我一生都无法弄清楚如何解决这个问题。如果有人可以给我一些帮助,我已经尝试了几个小时但没有任何进展......

import java.util.Scanner;

public class Appointment
{
protected int year = 0;
protected int month = 0;
protected int day = 0;
protected String description = "";

public Appointment(int year,int month ,int day, String description)
{
this.year = year;
this.month = month;
this.day = day;
this.description = description;
}

public static void main(String[] args)
{
Appointment[] appointments = new Appointment[4];
appointments[0] = new Daily(2011, 8, 13, "Brush your teeth.");
appointments[1] = new Weekly(2012, 2, 3, "Buy groceries.");
appointments[2] = new Monthly(2012, 5, 20, "Visit grandma.");
appointments[3] = new Onetime(2012, 11, 22, "Dentist appointment.");
//set all appointments as necessary.........
System.out.println("Enter a date (year month day) to list "
+ "appointments: ");
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
for (int i = 0; i < appointments.length; i++)
{
if (appointments[i].occursOn(year, month, day))
{
System.out.println(appointments[i]);
} // end if loop
} //end for loop
}//end main

public int getYear(int year)
{
int year2 = 0;
year2 = year;

return year2;
}

public int getMonth(int month)
{
int month2 = 0;
month2 = month;
return month2;
}

public int getDay(int day)
{
int day2 = 0;
day2 = day;
return day2;
}

public boolean occursOn(int year,int month,int day)
{
year = 0;
month = 0;
day = 0;
return false;
}

public String toString()
{
String nothing = "";
return nothing; // change this. Needs to return appointments for that specific day combination.
}
}

这是我的预约类(class)...其他文件是这个...

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

/**
Weekly appointment
*/
public class Weekly extends Appointment
{

/**
Initializes appointment for a given date
@param year the year
@param month the month
@param day the day
@param description the text description of the appointment
*/
public Weekly(int year, int month, int day, String description)
{
super(year, month, day, description);
}

/**
Determines if the appointment occurs on the same weekday
@param year the year
@param month the month
@param day the day
@return true if day matches the base appointment weekdate date and is
later than the base appointment
*/
public boolean occursOn(int year, int month, int day)
{
if (year < getYear())
{
return false;
}
if (month < getMonth() && year == getYear())
{
return false;
}
// we need to determine if the appointment is on the same day of the
// week, the GregorianCalendar class is useful for that
GregorianCalendar today = new GregorianCalendar(year, month, day);
GregorianCalendar appointment = new GregorianCalendar(getYear(),
getMonth(), getDay());

return today.get(Calendar.DAY_OF_WEEK) == appointment
.get(Calendar.DAY_OF_WEEK);
}
}

谢谢大家的帮助!!! (已修复!)

最佳答案

在您的Weekly类(class)中,您有:

if (year < getYear())

您正在调用不带参数的 getYear(),但它在 Appointment 中定义为采用一个 int 参数。

public int getYear(int year)
{
int year2 = 0;
year2 = year;

return year2;
}

根据该定义,您需要在调用该方法时将 int 传递给该方法。

您还可以更改 getYear() 的实现,以便它不接受参数,这是首选方法。您当前的实现似乎只是返回您传入的相同值。它可能应该返回在构造函数中设置的值 this.year

关于java - 从另一个类调用方法时遇到很多麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20077094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com