1

これは私が達成しようとしているものです

public class UDPThread extends Thread {

    int port;
    Spb_server station = null;


    public UDPThread(int port, Spb_server station) {
        this.port = port;
        this.station = station;
    }   

    public static void main(String[] args) {
        new UDPThread(5002,station).start();
    }  
}         

stationクラスの作成中のオブジェクトであり、メソッドSpb_serverでアクセスしたい。mainしかし、それは私がしstaticたくない修飾子を作るように私に求めます。これを達成する方法はありますか?

4

2 に答える 2

0

main からアクセスしたい場合は static にする必要があります。2 つの可能性:

int port;
static Spb_server station = null;


public UDPThread(int port, Spb_server station)
{
    this.port = port;
    this.station = station;
}   

public static void main(String[] args)
{
    new UDPThread(5002,station).start();
}

またはローカル変数:

int port;



public UDPThread(int port, Spb_server station)
{
    this.port = port;
    this.station = station;
}   

public static void main(String[] args)
{
    Spb_server station = null;
    new UDPThread(5002,station).start();
}
于 2013-11-07T01:48:57.157 に答える
0

さて、Spb_serverどこかを初期化する必要があります。このコードでは、メイン関数でそれを行うのが理にかなっているので、次のようなものです

public static void main(String[] args) {
    Spb_server station = new Spb_server() // Or whatever your constructor is
    new UDPThread(5002,station).start();
}
于 2013-11-07T04:38:28.333 に答える