Java方法重载:类型的方法未定义

时间:2015-04-28 18:20:25

标签: java overloading

我试图让命令提示询问用户一周中的哪一天,首先让他们输入当天的字符串,然后让他们输入当天的整数。我正在尝试重载方法'dayName',但Eclipse一直提示我为我的第一个方法分配一个类型。它说“方法dayName()未定义为dayName类型。我认为将它们设置为静态会有所帮助,但在这种情况下它不会。任何建议?提前感谢!

 /**
     * 
     */
    package mytime;
    import java.util.Scanner;
    /**
     * @author Ashley
     *
     */
    public class DayOfWeek {

    static String day;
    static String weekday;
    static Scanner input = new Scanner(System.in);
    static String monday = "Mondays are a bummer.";
    static String tuesday = "At least it isn't Monday!";
    static String wednesday = "Getting there.";
    static String thursday = "Almost there.";
    static String friday = "TGIF";
    static String saturday = "Party!";
    static String sunday = "Oh no, school tomorrow!";
    static String errorDay = "Not a valid day of the week.";

    public static dayName(String day) {

        if (day.equals("Sunday")){
            weekday = sunday;
        } 
        else if (day.equals("Monday")){
            weekday = monday;
        }
        else if (day.equals("Tuesday")){ 
            weekday = tuesday;
        }
        else if (day.equals("Wednesday")){
            weekday = wednesday;
        }
        else if (day.equals("Thursday")){
            weekday = thursday;
        }
        else if (day.equals("Friday")){
            weekday = friday;
        }
        else if (day.equals("Saturday")){
            weekday = saturday;
        }
        else{
            weekday = errorDay;
        }
        return weekday;
    }

    public static void main(String[] args) {

        System.out.println("Please enter day of the week:");
        day = input.nextLine();
        System.out.println(day);
        dayName weekday = new dayName();
        weekday.dayName();

        System.out.println(weekday);

        System.out.println("Please enter day of the week:");
        int day = input.nextInt();
        System.out.println(day);
        DayOfWeek(day);
        System.out.println(weekday);
    }
    public static dayName(int day){

        switch (day){
        case 1:
            weekday = sunday;  
            break;
        case 2:
            weekday = monday;  
            break;
        case 3:
            weekday = tuesday;  
            break;
        case 4:
            weekday = wednesday;  
            break;
        case 5:
            weekday = thursday;  
            break;
        case 6:
            weekday = friday;  
            break;
        case 7:
            weekday = saturday;  
            break;
        default:
            weekday = errorDay;
            break;
        }
        return weekday;

    }

}

2 个答案:

答案 0 :(得分:0)

您需要在方法调用中传递intString

weekday.dayName();

更改为

dayName(day);// because all are static method no need to create object

并且方法中也缺少返回类型。

 public static dayName(String day)

将其更改为

 public static String dayName(String day)

您需要为其他方法public static dayName(int day)做同样的事情。

因此您需要将逻辑更改为:

public static void main(String[] args) {

    System.out.println("Please enter day of the week:");
    day = input.nextLine();
    System.out.println(day);
      weekday= dayName(day);
    System.out.println(weekday);

    System.out.println("Please enter day of the week:");
    int day1 = input.nextInt();
    System.out.println(day1);
    weekday=dayName(day1);
    System.out.println(weekday);
}

答案 1 :(得分:0)

有些事情可能会导致您的问题。

简短回答

  1. String两种方法使用dayName()返回类型。
  2. 不要在main方法中使用dayName个对象,而是使用String代替
  3. 替换:

    day = input.nextLine();
    System.out.println(day);
    dayName weekday = new dayName();
    weekday.dayName();
    System.out.println(weekday);
    

    通过:

    day = input.nextLine();
    System.out.println(day);
    String weekday = dayName(day);
    System.out.println(weekday);
    

    解释

    未定义的班级dayName

    您的代码无法编译,因为您尝试使用未定义的类型(类):dayName

    旁注:类名应该用Java大写。

    在这段代码中:

    dayName weekday = new dayName();
    weekday.dayName();
    

    您假设dayName是一个类,但它不存在。也许您想使用DayOfWeek

    未定义的方法dayName()

      

    对于dayName

    类型,未定义dayName()方法

    您收到的错误消息是因为您尝试在对象dayName()(类型为weekday)上使用方法dayName。这是因为您的陈述weekday.dayName();

    问题是,你没有在那个类中定义这样的方法(我甚至没有看到类dayName,如第一部分所述)。

    dayName(int)dayName(String)

    没有返回类型

    您使用的语法不正确,无法声明方法:

    public static dayName(String day) {
        // ...
    }
    

    因为您没有提供任何返回类型。如果您不需要返回任何内容,请指定void返回类型,如下所示:

    public static void dayName(String day) {
        // ...
    }
    

    但是,您需要返回一个字符串,因此请String代替void