0

我在将变量传递给另一个类时遇到问题,因为它一直向我传递一个空白变量。

我想id从方法中获取值search()并将其转移到class foo方法内部total...

Boo

//method inside of class boo

public void search(){

try{

        String id = searchBox.getText();
        String idNum="";

        rs = stat.executeQuery("Select * from students where idNum='"+id+"'");

        while(rs.next()){

            idNum = rs.getString("idNum");

        }//while

            Members members = new Members();
            setVisible(false);

            members.setIdVal(id);

    }catch(Exception e){
        System.out.println("Error: "+e);
    }

}//search

Foo

// methods inside Foo
public void total(){

    System.out.println("Get: "+getIdVal());

    try{

    rs2 = stat2.executeQuery("SELECT * FROM paymentRecord where idNum ='"+getIdVal()+"';");

    }catch(Exception e){
        System.out.println("Total Error: "+e);
    }//

}//total

public void setIdVal(String val){

    this.val = val;

}//get the id

public String getIdVal(){

    //System.out.println("Inputted ID:" + val);
    return this.val;

}//get the id
4

1 回答 1

0

getIdVal()Foo类的方法,但应该是Boo类的方法。

class Boo {
   private String id;
   public void search(){
        // ...
        this.id = searchBox.getText(); 
        // ...
   }

   public int getIdVal() {
      return this.id
   }
}

Foo上课时:

Class Foo {
   public string Total(){
      Boo boo = new Boo();
      System.out.println(boo.getIdVal()); // will print the value from id searched in search() method
   }
}

您必须选择调用search()方法的位置。

于 2013-09-28T14:53:51.190 回答