有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将两个类合并在一起

我想将Vote4Cash类与commands类合并

我的vote4cash类将奖励玩家在键入命令检查时进行投票。因此,我需要将vote4cash进程构建到commands类中。我已经尝试了一段时间,这是我vote4cash系统中最重要的部分。如果你知道我如何把它们合并在一起,或者让它们一起工作,请告诉我怎么做。我试着用进口货,但没有成功。我没有像其他人那样编写commands类,这使得我很难添加vote4gold进程或使它们协同工作

我的Vote4Cash课程:

import java.sql.*;
import java.net.*;

public class Vote4Cash {

  public static void main(String args[]) {

  Connection con = null;
  Statement st = null;
  Statement stmt = null;
  ResultSet auth = null;
  ResultSet given = null;

    String url = "jdbc:mysql://localhost:3306/";
    String db = "vote4gold";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);
  st = con.createStatement();
  stmt = con.createStatement();

  //String give = "SELECT `given` FROM `has_voted` WHERE `ip` LIKE '+thisIp.getHostAddress()'";
        InetAddress thisIp =InetAddress.getLocalHost();
String give = "SELECT `given` FROM `has_voted` " + 
              "WHERE `ip` = '" + thisIp.getHostAddress() + "'";
  given = st.executeQuery(give);

while (given.next()) {
    if (given.getInt("given") > 0) {
        System.out.println("You've already recieved a reward for the last time you voted, but thanks again for voting.");
    } else {
        System.out.println("Thanks for voting! You've been rewarded 25m gold! Vote again tomorrow!");
        String sql = "SELECT has_voted (given) Replace('0', '0', '1')";
        int rows = stmt.executeUpdate("UPDATE has_voted SET given = 1 WHERE given = 0");
        System.out.println("The given reward column has been set to 1 for the ip address:");
       System.out.println("IP:"+thisIp.getHostAddress());
    }
  }
  } catch (Exception e) {
  System.out.println(e);
  }
  }
}

我的命令类(此处不适用): http://pastebin.com/GXFLfMX4

谢谢!如果您需要更多详细信息或不了解某些内容,请告诉我,以便我能帮助您。:)


共 (1) 个答案

  1. # 1 楼答案

    首先,我不会尝试“合并”代码。当你从别人那里收到代码时,这永远不应该是解决方案。你应该首先试着理解它的用途

    为此

    首先,声明Commands类位于: package server.model.players.packets;

    所以你需要导入这个包。此外,Commands类中的方法将Client作为参数。因此,为了使用这个类,您需要能够实例化Client的一个实例

    那是哪一个?嗯,看看Commands中的import语句,它似乎是server.model.players.Client。您需要访问该类并能够创建实例

    您是否获得了Commands的源代码、类的编译集或jar?如果是前者,则需要访问Commands导入的所有其他类才能编译它

    我怀疑,看看Commands类的冗长,以及你想把它当作一个黑匣子来对待(但还是要把它和你的类合并在一起?!?(这实际上只是一个main方法的包装器),你真的只是在寻找一个快速的解决方案。我会从小处做起。抵制合并的冲动。尝试编写一个Commands自己的类,使用两个简单的方法来获取您设计的类。你最终会有一个你理解的过程,并获得更好的体验