简体   繁体   English

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

[英]How can I use a method in one of my other classes in my current class

Greetings:I have a problem trying to use a method in my GUI class that i declared in another class. 问候:我尝试在我的GUI类中使用我在另一个类中声明的方法时遇到问题。

This is the method in my Memba class that i'm trying to use: 这是我正在尝试使用的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;  
}

The method creates a new member. 该方法创建一个新成员。

In my GUI class >>Register class<< i'm trying to use the method and i can't get it to work.I have tried creating instances,like so: 在我的GUI类>>注册类<<我正在尝试使用该方法,我无法让它工作。我尝试创建实例,如下所示:

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

The code already gets data that is entered in the textfields. 代码已经获取在文本字段中输入的数据。

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();

Please help me resolve this problem.Regards. 请帮我解决这个问题。请注意。

You need to use a constructor for that, or a method returning an instance of Memba , instead of void . 您需要使用构造函数,或者返回Memba实例的方法,而不是void

Constructor 构造函数

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;  
}

Method delivering new instance (usually in factory patterns and such like) 提供新实例的方法 (通常以工厂模式等)

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;
}

Notes 笔记

  • My advice is to stick to conventions regarding variable names - ie Memb_Name should be refactored as name or memberName , etc. 我的建议是坚持有关变量名的约定 - 即Memb_Name应该重构为namememberName等。
  • In the last assignments in the void method you expose in your question, you seem to assign static fields with the parameters of your method. 在您在问题中公开的void方法的最后一个赋值中,您似乎使用方法的参数指定static字段。 I am assuming here they are in fact instance fields of Memba 我在这里假设它们实际上是Memba实例字段

There are two options, using your non-static void method you would need an instance to call newMember on. 有两个选项,使用非静态void方法,您需要一个实例来调用newMember Like, 喜欢,

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

Alternatively, change newMember to return a Memba (and be static ), like 另外,改变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;
}

Then you could use it like, 然后你可以像使用它一样

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

i resolved my problem above by create a constructor that i was able to re use in my GUI. 我通过创建一个能够在我的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;


    }

i re used it as 我用它作为

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现将文件加载到程序中的其他类可以使用的类? - How do I implement a class that loads a file into my program that other classes can use? Java:如何将方法重构为类,然后在其他类中使用其参数? - Java: How can I refactor a method to a class and then use its parameters in other classes? 如何直接从其他类的库类中访问方法? Java的 - How can I directly access methods from my library class in other classes? Java 如何将JAR文件中的外部类用作程序中的类之一? - How to use an external class in a JAR file as one of the classes in my program? 我的main方法中的哪条语句正在调用我的其他类和main类中的所有其他方法? - Which statement in my main method is calling all my other methods in my other classes and my main class? 如何让我的按钮在其他类中可见 - How can i make my buttons visible in other classes 我可以在我的测试 class 的一种方法中使用 @MockBean 来模拟 bean 吗? - Can I use @MockBean to mock a bean in just one method of my test class? 如何使用片段方法? - How can I use my fragment method? Kryonet - 如何在课堂上注册课程? - Kryonet — How can I register classes within my class? 如何确定我的课程使用多少内存? - How can I determine how much memory my classes use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM