1

私のクラスは次のようになります。

public class Month
 {
private int numOfMonth;
private int monthNum;

public int monthNum()
{
    return monthNum = 1;
}

public void setMonthNum(int monthNum){

    switch (monthNum)
    {
    case 1: System.out.println("January"); break;
    case 2: System.out.println("February");break;
    case 3: System.out.println("March");break;
    case 4: System.out.println("April");break;
    case 5: System.out.println("May");break;
    case 6: System.out.println("June");break;
    case 7: System.out.println("July");break;
    case 8: System.out.println("August");break;
    case 9: System.out.println("September");break;
    case 10: System.out.println("October");break;
    case 11: System.out.println("November");break;
    case 12: System.out.println("December");break;
    }

}

    public String getName() 
    {
        return "" + monthNum;
    }

}

私のドライバーは次のとおりです。

import java.util.Scanner;

public class monthDriver
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter month number: ");
    int monthNum = in.nextInt();

    System.out.println("Month number " + monthNum + " is the month of " + Month.getName());

}
 }

コンパイル時エラーが発生します:

"monthDriver.java:12: error: non-static method getName() cannot be referenced from a static context
    System.out.println("Month number " + monthNum + " is the month of " + Month.getName());1 error"

私は学生であり、学問的誠実性は私にとって重要であることに留意してください。なぜこのようなエラーが表示されるのですか? また、将来のコーディング効率を改善するための提案はありますか? 皆様のお時間とご尽力に感謝いたします。それは歓迎です。

4

4 に答える 4

1

まず、クラス自体を最初にインスタンス化せずに、Month.getName()を使用して直接、クラスのメソッド(この場合はMonth)にアクセスする場合は、そのメソッドを静的として定義する必要があります。

クラスで静的メソッドまたは非静的メソッドをいつ使用するかについては、ライブラリを埋めるためにオンラインで非常に多くの記事を見つけることができます:-)

上記のコードに関するもう1つの小さなメモ。スイッチを使用する代わりに、列挙型を使用することをお勧めします。

于 2013-02-21T16:41:02.270 に答える
1

方法 1:

次のように static を palcing することで問題を解決できます。

public static String getName() 
    {
        return "" + monthNum;
    }

そして、あなたは次のようにする必要があります

System.out.println("Month number " + monthNum + " is the month of " + Month.getName());

方法 2 :

のオブジェクトを作成しclass Month、次に :

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter month number: ");
    int monthNum = in.nextInt();
    Month obj=new Month();
    System.out.println("Month number " + monthNum + " is the month of " + obj.getName());

}
于 2013-02-21T16:40:25.573 に答える
0

私はあなたのプログラムを修正したり、あなたのプログラムを強化したりしませんが、基本的な概念とあなたが間違っていることを理解する必要があります.

クラス別名オブジェクトのすべてのインスタンスに共通の変数を作成したい場合があります。同様に、静的メソッドも定義します

このリンクから読んでください。ここにスニペットを貼り付けているだけです。

Java プログラミング言語は、静的メソッドと静的変数をサポートしています。宣言に static 修飾子を持つ静的メソッド** は、クラスのインスタンスを作成する必要なく、クラス名で呼び出す必要があります。

 ClassName.methodName(args)

インスタンス変数とクラス変数およびメソッドのすべての組み合わせが許可されているわけではありません。

インスタンス メソッドは、インスタンス変数とインスタンス メソッドに直接アクセスできます。

インスタンス メソッドは、クラス変数とクラス メソッドに直接アクセスできます。

クラス メソッドは、クラス変数とクラス メソッドに直接アクセスできます。

クラス メソッドは、インスタンス変数またはインスタンス メソッドに直接アクセスできません。オブジェクト参照を使用する必要があります。また、参照する this のインスタンスがないため、クラス メソッドは this キーワードを使用できません。

あなたの場合、これはインスタンスメソッドであるため、最後のルールが適用されるため、 Month.setMonthnum(bla) を呼び出すことはできません

  public void setMonthNum(int monthNum)
于 2013-02-21T17:12:15.590 に答える
0

あなたのコードは少し未加工で乱雑に見えます。私は使用をお勧めします:

    public static String getMonthNameForNum(int monthNum) {
    switch (monthNum) {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            return "UNKNOWN";
    }
}

そしてそれを次のように使用します:

        System.out.println("Month number " + monthNum + " is the month of " + Month.getMonthNameForNum(monthNum));

PS もちろん、これは教育用のコードにすぎません。実際には、標準のカレンダーなどを使用して月の名前を取得します。

于 2013-02-21T16:45:52.700 に答える