如何在不使其静态的情况下调用类

时间:2012-02-11 06:53:57

标签: java

我试图调用类Player,但它要求我将一些方法更改为静态,从而创建800个警告并打破整个服务器。 唯一的解决方案是修复800警告吗? (告诉我以静态方式访问该方法?

有几点需要注意: 我需要在没有将Player播放器放入方法中的情况下执行此操作: public void methodname(Player player){ 并且不使用构造函数就可以,因为我已经拥有的构造函数不能应用于我想要的代码。

整个方法就是这个

public static void main(String args[]) throws Exception {
    //args = new String[] { "true", "false", "true" };
    if (args.length != 3) {
        System.err.println("USE: guimode(boolean) debug(boolean) hosted");
        return;
    }
    Config.HOSTED = Boolean.parseBoolean(args[2]);
    Config.DEBUG = Boolean.parseBoolean(args[1]);
    long currentTime = System.currentTimeMillis();
    Logger.log("Launcher", "Initiating Cache...");
    Cache.init();
    ItemEquipIds.init();
    Huffman.init();
    Logger.log("Launcher", "Initiating Data Files...");
    IPBanL.init();
    PkRank.init();
    TeleportLocations.initTeleportLocations();
    DTRank.init();
    MapContainersXteas.init();
    MapAreas.init();
    ObjectSpawns.init();
    NPCSpawns.init();
    NPCCombatDefinitionsL.init();
    NPCBonuses.init();
    NPCDrops.init();
    ItemExamines.init();
    ItemBonuses.init();
    ShopsHandler.init();
    NPCDefinitions.getNPCDefinitions(20);
    Logger.log("Launcher", "Initiating Fishing Spots...");
    FishingSpotsHandler.init();
    Logger.log("Launcher", "Initiating NPC Combat Scripts...");
    CombatScriptsHandler.init();
    Logger.log("Launcher", "Initiating Dialogues...");
    DialogueHandler.init();
    Logger.log("Launcher", "Initiating Controlers...");
    ControlerHandler.init();
    Logger.log("Launcher", "Initiating Cutscenes...");
    CutscenesHandler.init();
    Logger.log("Launcher", "Initiating Friend Chats Manager...");
    FriendChatsManager.init();
    Logger.log("Launcher", "Initiating Cores Manager...");
    CoresManager.init();
    Logger.log("Launcher", "Initiating World...");
    World.init();
    Logger.log("Launcher", "Initiating Region Builder...");
    RegionBuilder.init();
    Logger.log("Launcher", "Initiating Server Channel Handler...");
    NPCSpawner.spawnNPCS();
    try {
        ServerChannelHandler.init();
    } catch (ChannelException e) {
        e.printStackTrace();
        Logger.log("Launcher",
                "Failed Initiating Server Channel Handler. Shutting down...");
        System.exit(1);
        return;
    }
    // Logger.log("Launcher", "Initiating Server Manager...");
    // ServerManager.main(null);
    System.err.println("Server took "
            + (System.currentTimeMillis() - currentTime)
            + " miliseconds to start up.");
    addAccountsSavingTask();
    addCleanMemoryTask();
    for ( ;; )
    {
       try {
            Thread.sleep(10000);
            player.sendMessage("[Server Message]: Remember to vote!");
            Thread.sleep(10000);
            player.sendMessage("[<img=1>Server Message<img=1>]: Donate to Lubricant and ONLY Lubricant!");
           } catch (InterruptedException e) {
            e.printStackTrace();
          }
    }
}

另请注意,我无法更改构造函数播放器,或者在其中没有启动任何内容的新构造函数(I.E public Player {}) 这是我的播放器构造函数:

公共类播放器扩展实体{

public Player(String password) {
    super(Config.START_PLAYER_LOCATION);
    trampAmount = 1;
    setHitpoints(100);
    this.password = password;
    appearence = new Appearence();
    inventory = new Inventory();
    equipment = new Equipment();
    skills = new Skills();
    combatDefinitions = new CombatDefinitions();
    prayer = new Prayer();
    bank = new Bank();
    controlerManager = new ControlerManager();
    musicsManager = new MusicsManager();
    emotesManager = new EmotesManager();
    friendsIgnores = new FriendsIgnores();
    dominionTower = new DominionTower();
    charges = new ChargesManager();
    runEnergy = 100;
    allowChatEffects = true;
    mouseButtons = true;
    pouches = new int[4];
    killedBarrowBrothers = new boolean[6];
    SkillCapeCustomizer.resetSkillCapes(this);
}

1 个答案:

答案 0 :(得分:1)

不确定如何在具有main()方法的类中创建Player类的对象。由于您正在创建main方法范围之外的对象(本质上是静态的),它告诉您不能使用静态上下文中的非静态变量。因此,您可以将static关键字添加到Player变量中,以使其工作。请检查代码段,并检查,是否也可以在您的情况下使用。

public class StaticVariable
{
    static One one = new One("New Password"); // This is what I mean, you can do.

    public static void main(String... args)
    {       
        StaticVariable.one.callMe(); // That's how you will call your methods now.
    }
}

class One
{
    String password;

    public One(String p)
    {
        password = p;
    }

    public void callMe()
    {
        System.out.println("I am callMe method of class One.");
        System.out.println(password);
    }
}

否则你必须提供具有主要方法的类的良好SSCCE