如何在我当前班级的其他课程中使用方法

时间:2014-10-17 12:47:28

标签: java

问候:尝试在我的GUI类中使用我在另一个类中声明的方法时遇到问题。

这是我正在尝试使用的Memba类中的方法:

public void newMember(String Memb_Name,String Memb_Surname,int age,int contact,String address,String username,String password){
    this.Memb_Name = Memb_Name;
    this.Memb_Surname = Memb_Surname;
    this.age = age;
    this.contact = contact;
    this.address = address;
    Memba.username = username;
    Memba.password = password;  
}

该方法创建一个新成员。

在我的GUI类>>注册类<<我正在尝试使用该方法,但我无法使其工作。我尝试过创建实例,如下所示:

Memba mmb = new Memba();
Memba mem= new newMember();
Memba mm = new Memba(s1,s2,s3,s4,s5,s6,s7);

代码已经获取在文本字段中输入的数据。

String s1 = nameFld.getText();
String s2 = surnameFld.getText();
int s3 = Integer.parseInt(ageFld.getText());
int s4 = Integer.parseInt(contactFld.getText());
String s5 = addressFld.getText();
String s6 = usernameFld.getText();
String s7 = passwordFld.getText();

请帮我解决这个问题。请注意。

3 个答案:

答案 0 :(得分:3)

您需要使用构造函数或返回Memba实例的方法,而不是void

构造

public Memba(String Memb_Name,String Memb_Surname,int age,int contact,String address,String username,String password) {

    this.Memb_Name = Memb_Name;
    this.Memb_Surname = Memb_Surname;
    this.age = age;
    this.contact = contact;
    this.address = address;
    this.username = username;
    this.password = password;  
}

提供新实例的方法(通常采用工厂模式等)

public static Memba newInstance(String Memb_Name,String Memb_Surname,int age,int contact,String address,String username,String password) {

    // you will need to declare an empty constructor in your Memba class, 
    // _if_ you have already declared the constructor above, 
    // for the following line to compile
    Memba result = new Memba(); 
    result.Memb_Name = Memb_Name;
    result.Memb_Surname = Memb_Surname;
    result.age = age;
    result.contact = contact;
    result.address = address;
    result.username = username;
    result.password = password;  
    return result;
}

备注

  • 我的建议是坚持conventions关于变量名称 - 即Memb_Name应该重构为namememberName等。
  • 在您在问题中公开的void方法的最后一次分配中,您似乎为static字段指定了方法的参数。我在这里假设它们实际上是Memba
  • 的实例字段

答案 1 :(得分:0)

有两个选项,使用非静态void方法,您需要一个实例来调用newMember。像,

Memba mmb = new Memba();
mmb.newMember(s1,s2,s3,s4,s5,s6,s7);

或者,更改newMember以返回Memba(并static),例如

public static Memba newMember(String Memb_Name,String Memb_Surname,
        int age,int contact,String address,String username,String password){
    Memba m = new Memba();
    m.Memb_Name = Memb_Name;
    m.Memb_Surname = Memb_Surname;
    m.age = age;
    m.contact = contact;
    m.address = address;
    m.username = username;
    m.password = password;
    return m;
}

然后你就可以使用它,

Memba mmb = Memba.newMember(s1,s2,s3,s4,s5,s6,s7);

答案 2 :(得分:0)

我通过创建一个能够在我的GUI中重用的构造函数来解决上面的问题。

public Memba(String MembsName,String MembsSurname,int ages,int contacts,String addresses,String usernames,String passwords){


        this.MembName = MembsName;
        this.MembSurname = MembsSurname;
        this.age = ages;
        this.contact = contacts;
        this.address = addresses;
        Memba.username = usernames;
        Memba.password = passwords;


    }

我把它用作

String s1 = nameFld.getText();
            String s2 = surnameFld.getText();
            int s3 = Integer.parseInt(ageFld.getText());
            int s4 = Integer.parseInt(contactFld.getText());
            String s5 = addressFld.getText();
            String s6 = usernameFld.getText();
            String s7 = passwordFld.getText();

            new Memba(s1,s2,s3,s4,s5,s6,s7);