【问题标题】:Getting Values of 3 to 5 Interger Variables from a difrerent Class in JAVA从 JAVA 中的不同类获取 3 到 5 个整数变量的值
【发布时间】:2020-04-10 14:26:15
【问题描述】:

我目前正在为我们的 JAVA 训练营做一个迷你停车系统。

我正在尝试将整数变量的值放入不同的类中。 这些变量是总停放的 sql 计数的结果(countCarcountMotorcountVan)。

这就是我所做的:

我将它们存储在一个名为 SQLCountResult 的类中:

public class SQLConnections {
    public Connection con = null;

    public void parkInSQL() {
        con = dbConnect.con();

        int countCar;
        int countMotor;
        int countVan;

        int parkCar;
        int parkMotor;
        int parkVan;

        int[] vehicleTotal = new int[3];

        String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
        String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
        String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";

        try {
            Statement stmtCar = con.createStatement();
            Statement stmtMotor = con.createStatement();
            Statement stmtVan = con.createStatement();

            ResultSet rsCar = stmtCar.executeQuery(qCar);
            ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
            ResultSet rsVan = stmtVan.executeQuery(qVan);

            rsCar.next();
            rsMotor.next();
            rsVan.next();

            countCar = rsCar.getInt(1);
            countMotor = rsMotor.getInt(1);
            countVan = rsVan.getInt(1);

        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
}

现在我想致电countCarcountMotorcountVan 到另一个名为 Park 的班级。我的 Park 类代码如下:

public class Park {

    public Connection con = null;

    public void parkMethod() {
        SQLConnections sqlConnections = new SQLConnections();
        sqlConnections.parkInSQL();
    }

}

我也尝试过使用扩展,但这也不起作用。如何将这些变量调用到 Park 类?

【问题讨论】:

    标签: java variables methods automation integer


    【解决方案1】:
    public class VehicleStatistics {
      private int countCar;
      private int countMotor;
      private int countVan;
    
      public VehicleStatistics(int countCar, int countMotor, int countVan) {
        this.countCar = countCar;
        this.countMotor = countMotor;
        this.countVan = countVan;
      }
    
      public int getCountCar() {
        return countCar;
      }
    
      public void setCountCar(int countCar) {
        this.countCar = countCar;
      }
    
      public int getCountMotor() {
        return countMotor;
      }
    
      public void setCountMotor(int countMotor) {
        this.countMotor = countMotor;
      }
    
      public int getCountVan() {
        return countVan;
      }
    
      public void setCountVan(int countVan) {
        this.countVan = countVan;
      }
    }
    

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class VehicalesDAOImpl {
      private Connection connection;
    
      public VehicalesDAOImpl(Connection connection) {
        this.connection = connection;
      }
    
      public VehicleStatistics getVehicleStatistics() {
        String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
        String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
        String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";
        VehicleStatistics result = null;
        try {
          Statement stmtCar = connection.createStatement();
          Statement stmtMotor = connection.createStatement();
          Statement stmtVan = connection.createStatement();
    
          ResultSet rsCar = stmtCar.executeQuery(qCar);
          ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
          ResultSet rsVan = stmtVan.executeQuery(qVan);
    
          rsCar.next();
          rsMotor.next();
          rsVan.next();
    
          int countCar =rsCar.getInt(1);
          int countMotor =rsMotor.getInt(1);
          int countVan =rsVan.getInt(1);
          result = new VehicleStatistics(countCar, countMotor, countVan);
        } catch(SQLException e) {
          //log error
        } finally {
          //close connections etc...
        }
        return result;
      }
    }
    

    【讨论】:

      【解决方案2】:

      我建议你先对class membersusing objects做一些补充阅读。如果您不熟悉对象,请阅读这篇名为 What Is an Object? 的综合文章。我不能强调理解这一点的重要性。 “面向对象编程”的概念以对象为中心。熟悉此内容后,请继续阅读我的其余答案。


      既然您已经熟悉了我上面引用的文章,我将从设置一些字段开始,这些内容在“声明成员变量”一文中进行了说明。首先我们需要问自己,“我们试图在Park 类中检索哪些变量?”正如您在问题中提到的,相关变量是countCarcountMotorcountVan

      我们已经确定了我们所追求的变量。怎么办?下一步是将这些变量更改为字段,以便当您的 parkInSQL 方法返回时它们不会被 garbage collector 吞噬。这是一个简单的任务。让我们首先从 parkInSQL 方法中删除变量:

      public void parkInSQL() {
              con = dbConnect.con();
      
              ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶C̶a̶r̶;̶
              ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶M̶o̶t̶o̶r̶;̶
              ̶ ̶i̶n̶t̶ ̶c̶o̶u̶n̶t̶V̶a̶n̶;̶
      
              //...
      }
      

      接下来我们需要将这些变量声明为字段。为此,只需将变量声明放在类的顶部,就像使用 Connection 变量一样:

      public class SQLConnections {
          public Connection con = null;
          public int countCar;
          public int countMotor;
          public int countVan;
      
          //...
      }
      

      请注意,我使用了 public access modifier。这是为了确保这些字段对我们的 Park 类可见(我们可以将它们设为私有,但这会让我们进入 getters and setters,这是另一节课)。

      一旦我们将变量声明为字段,我们就应该使用 SQLConnections 类进行设置。剩下的就简单了。

      你已经完成了一半的工作。如果您查看parkMethod,您会看到您构造了一个 SQLConnections 对象:

      SQLConnections sqlConnections = new SQLConnections();
      

      现在我们需要做的就是引用新创建的字段,这在我上面引用的“使用对象”文章中进行了解释。调用sqlConnections.parkInSQL()后,可以引用sqlConnections.countCarsqlConnections.countMotorsqlConnections.countVan的字段:

      public class Park {
      
          public Connection con = null;
      
          public void parkMethod() {
              SQLConnections sqlConnections = new SQLConnections();
              sqlConnections.parkInSQL();
      
              int countCar = sqlConnections.countCar;
              int countMotor = sqlConnections.countMotor;
              int countVan = sqlConnections.countVan;
          }
      
      }
      

      现在您可以对这些值进行相应的操作了。

      【讨论】:

        【解决方案3】:

        在方法中定义的变量是该方法的本地变量。
        如果你想在类/方法之间共享变量,那么你需要将它们指定为类的成员变量。您应该在方法之外对其进行初始化。

        public class SQLConnections { 
        
            public int countCar;
            public int countMotor;
            public int countVan;
        
            //...
        }
        

        【讨论】:

          【解决方案4】:

          您可以更改变量的范围。

          移动这个:

              int countCar;
              int countMotor;
              int countVan;
          

          parkInSQL 方法之外,并添加访问修饰符public:

              public int countCar;
              public int countMotor;
              public int countVan;
          

          然后你可以像这样从Park 类访问它:

          public class Park {
          
              public Connection con = null;
          
              public void parkMethod() {
                  SQLConnections sqlConnections = new SQLConnections();
                  int iAmUsingCountCar = sqlConnections.countCar;
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2021-11-11
            • 1970-01-01
            • 2012-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-30
            • 1970-01-01
            相关资源
            最近更新 更多