-2

あるクラスから別のクラスに値 t を渡そうとしていますが、プログラムを実行する前に、non static method cannot be referenced from static context次のコード行から取得します:

t = (PrinterSettings.getT() * 60);

私はこのコードから値 t を取得しようとしています:

public int t = 1; //defualt value for amount of mintues in the future the job should wait untill sent

public int getT() {
            return (t);
        }

 public void setT(int t) {
            this.t = t;
         } 

私は何を間違えましたか?どうすればtを取得できますか

編集 :

t を取得するコード全体

         public int t = 1; //defualt value for amount of seconds in the future the job should wait untill sent

    public int getT() {
        return (t);
    }

    public void setT(int t) {
        this.t = t;
    }

そして、これは私が使用しているクラスで、上記のクラスから t を呼び出して使用します:

public class DealyTillPrint {

    public int t;

    public String CompletefileName;
    private String printerindx;
    private static int s;
    private static int x;
    public static int SecondsTillRelase;

    public void countDown() {
        System.out.println("Countdown called");
        s = 1; // interval 
    t = ((new PrinterSettings().getT()) * 60); //(PrinterSettings.SecondsTillRelase); // number of seconds
        System.out.println("t is : " + t);
        while (t > 0) {
            System.out.println("Printing in : " + t);
            try {
                Thread.sleep(s * 1000);
            } catch (Exception e) {
            }
            t--;
        }

ここでt、スピナーを使用して設定します

<p:spinner min="1" max="1000" value="#{printerSettings.t}"  size ="1">
                    <p:ajax update="NewTime"/>
                </p:spinner>
4

3 に答える 3

2

を使用していますが、はクラスであり、メソッドはオブジェクト用であるPrinterSettings.getT()ため、これを行うことはできません。最初にPrinterSettingsのオブジェクトを作成する必要があります。次に、を呼び出すことができます。PrinterSettingsgetT()getT()

PrinterSettings myObjectOfPrinterSettings = new PrinterSettings();
myObjectOfPrinterSettings.getT();  //this should work without the error
于 2013-02-11T18:59:33.517 に答える
1

次の2つのいずれかを選択できます。

1)PrinterSettingsファイル内のすべてを静的にします(そしてPrinterSettingsも静的にします):

public static int t = 1; 

public static int getT() {
            return (t);
        }

 public static void setT(int t) {
            this.t = t;
         } 

2)PrinterSettingsを変更せず、コードに対してこれを実行します。

//Put this somewhere at the beginning of your code:
PrinterSettings printerSettings = new PrinterSettings();

//Now have some code, which will include setT() at some point

//Then do this:
t = (printerSettings.getT() * 60);

私の意見では、後者の方が望ましいでしょう。

編集:私が今行った編集は、使用していたPrinterSettings変数を保持しない場合t、その新しいPrinterSettingsオブジェクトで1つ上の変数が1になるためです。代わりに、プログラムの最初にPrinterSettingsのオブジェクトをインスタンス化していることを確認し、それを最後まで使用してください。

于 2013-02-11T18:59:43.357 に答える
0

それ以外の:

public int getT() {
            return (t);
        }
Write:

public static int getT() {
            return (t);
        }

これで問題が解決します。この変更により、クラス名を使用してこのメ​​ソッドにアクセスできます。クラスメソッドとして。

于 2013-02-11T19:00:47.250 に答える