-1

まず、私は持っています

if (playerCommand.startsWith("yell")) {
            Yell();  

    }

「commands.java」という名前のクラス このクラスは、入力コマンドを処理します。この特定のコマンドのメソッドを他の場所にあるクラスに入れたいのですが、コードは

public class Yell {
/*
 * The minimum rights required to use the yell channel
 */
private final static int MINIMUM_RIGHTS_REQUIREMENT = 0;

/*
 * Time in seconds between the client must wait between submitting yells
 */
private final static int SECONDS_BETWEEN_SUBMITION = 3;

/*
 * Minimum player rights at which restrictions are ignored
 */
private final static int BYPASS_LIMITATION_RIGHTS = 6;


/*
 * An enumeration containing format data
 */
static enum yellData {
    /* Rights, Color, Title */
    PLAYER(0,"255","Player"),
    SPECIAL(1,"16730502","Special"),
    VETERAN(2,"13132800","Veteran"),
    DONATOR(3,"42575","Donator"),
    SUPER_DONATOR(4,"3327","Super Donator"),
    EXTREME_DONATOR(5,"1800639","Extreme Donator"),
    TRIAL_MODERATOR(6, "11809016", "Trial Mod"),
    MODERATOR(7, "562688", "Mod"),
    ADMIN(8, "15604003", "Admin"),
    HEAD_ADMIN(9, "16274188", "Head Admin"),
    CO_OWNER(10, "13369344", "Co-Owner"),
    OWNER(11, "13369344", "Owner");

    private int rights;
    private String color, title;

    private yellData(int rights, String color, String title) {
        this.rights = rights;
        this.color = color;
        this.title = title; 
    }

    private int getRights() {
        return rights;
    }
    private String getColor() {
        return color;
    }
    private String getTitle() {
        return title;
    }

}

public static void sendYellMessage(final Client c, String text) {
    /* Restrictions - Do not apply to rights under Bypass Limitation Rights */
    if (c.playerRights <= BYPASS_LIMITATION_RIGHTS) {
        if (Connection.isMuted(c)) {
            c.sendMessage("You are muted.");
            return;
        }
        if (c.playerRights < MINIMUM_RIGHTS_REQUIREMENT) {
            c.sendMessage("You do not have permission to use the yell channel.");
            return; 
        }
        if (c.playerCanYell == false) {
            c.sendMessage("Please wait a few seconds between yells.");
            return;
        }

        /* Formatting string prevention */
        String[] forbidden = {"<col", "<shad", "<img", "<str", "<tran", "tradereq", "duelreq", "clanreq", "chalreq", "@str@", "@"}; 
        for (int i = 0; i < forbidden.length; i++) {
            if (text.substring(1).contains(forbidden[i])) {
                c.sendMessage("Your submission contains illegal phrases or characters.");
                System.out.println(formatPlayerName(c.playerName)+" is attempting to use illegal phrases in yell chat.");
                return;
            }
        }
    }

    /* Generates the yell message */
    String toSend = null;
    for (final yellData rank : yellData.values()) {
        if (c.playerRights == rank.getRights()) {
            String prefix = "[<col="+rank.getColor()+">"+rank.getTitle()+"</col>"+"]";
            String name = "<col=255>"+formatPlayerName(c.playerName)+"</col>";
            String message = formatChat(text.replaceAll("/", ""));
            toSend = prefix+" "+name+": "+message;
        }
    }

    /* Send the message to all online players */
    for (int j = 0; j < PlayerHandler.players.length; j++) {
        if (PlayerHandler.players[j] != null) {
            Client op = (Client) PlayerHandler.players[j];
            op.sendMessage(toSend);
        }
    }

    /* Prevent the client from flooding */
    c.playerCanYell = false;
    EventManager.getSingleton().addEvent(new Event() {

        @Override
        public void execute(EventContainer e) {
            c.playerCanYell = true;
            e.stop();
        }
    }, SECONDS_BETWEEN_SUBMITION * 1000 );

    /* Truncate data after use to save memory */
    toSend = null;
}

/*
 * Capitalizes the first letter between spaces
 * Used to properly format character names
 */
private static String formatPlayerName (String in) {
    final StringBuilder output = new StringBuilder(in.length());
    String[] words = in.split("\\s");
    for(byte b=0,l=(byte) words.length;b<l;++b) {
      if(b>0) output.append(" ");      
      output.append(Character.toUpperCase(words[b].charAt(0)))
            .append(words[b].substring(1));
    }
    return output.toString();
}

/*
 * Capitalizes the first letter and does not disturb
 * the rest of the sentence capitalizations
 */
private static String formatChat (String in) {
    return in.substring(0,1).toUpperCase() + in.substring(1);
}

Commands.java ファイルでクラス "Yell.java" を呼び出すにはどうすればよいですか? オブジェクトとして宣言されたコマンドで大声で叫びました

Yell = new Yell();
4

3 に答える 3

1

use Yell yell = new Yell(); // Yell クラスのオブジェクトを作成します。そして yell.method(); // これは Yell クラスからメソッドを呼び出します。

于 2013-04-08T14:03:25.887 に答える
1

作成したオブジェクトの参照を使用する

Yell yellObject = new Yell(); 
yellObject.methodName(); // Use appropriate methodName

new Yell()参照変数によってアクセスされるオブジェクトを作成します。作成されたオブジェクトでメソッドを呼び出すために同じことが使用されます。yellObject作成されたオブジェクトへの参照は次のとおりです。

于 2013-04-08T13:57:04.550 に答える
0
Yell y = new Yell();

y.methodCommand();

Commands.java クラスのメソッドが static の場合、クラス名で直接呼び出すことができます:Like

Yell.methodName():
于 2013-04-08T13:57:44.990 に答える