简体   繁体   English

在另一个 class 中使用 object

[英]Use an object in another class

I want to use the object person in the Librarian_Interface class, but this call the method login() in a loop.我想在 Librarian_Interface class 中使用 object 人,但这会在循环中调用方法 login()。 I think there is something I don't understand with java instance and I tried to get person with a constructor or methode but in vain.我认为 java 实例有一些我不明白的地方,我试图用构造函数或方法来找人,但徒劳无功。 Thanks !谢谢 !

public class Login_Interface {
    Person person;

    public Login_Interface() {
        
        db.initConnection();
        person = login(db, in);
        
        if (person != null)
        {
            Librarian_Interface a = new Librarian_Interface();
            a.run();
        }
    }

    public void run() {
    }

    public static Person login(DbConnection db, Scanner sc) {
        Persons.setDbConnection(db);
        Persons persons = Persons.getInstance();

        System.out.print("\nEnter your Phone Number : ");
        String phone = sc.nextLine();

        System.out.print("\nEnter your Password : ");
        String password = sc.nextLine();

        return persons.login(phone, password);
    }

}
public class Librarian_Interface {

public Librarian_Interface() {

// What I want 
// System.out.print(person); or person.getAge(); ...

All you have to do is pass the person object to the Librarian_Interface constructor.您所要做的就是将person object 传递给Librarian_Interface构造函数。 then you can call methods getAge on it, etc.然后你可以在它上面调用方法getAge等等。

public class Librarian implements Runnable {

    private final Person person;

    public Librarian(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        int age = person.age();
        // ... whatever else
    }
}

By the way, the convention in Java is to use upper camel case for class names, with no underscores.顺便说一句,Java 中的约定是对 class 名称使用大写的驼峰式大小写,不带下划线。 So it would be better to name the class LibrarianInterface .所以最好命名为 class LibrarianInterface Also, it's probably not the best idea to call it LibrarianInterface if it is in fact a class and not an interface .此外,如果它实际上是class而不是interface ,则称它为LibrarianInterface可能不是最好的主意。

Since Librarian has a public run method, it's a good idea to have it implement Runnable so that users of the class see how it is meant to be used.由于Librarian有一个公共的run方法,最好让它实现Runnable以便 class 的用户了解它的使用方式。

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

相关问题 Java:如何使用另一个类中的对象 - Java: how to use an object from another class 如何使用在另一个类中返回的对象 - How to use an object that got return in another class 创建具有与另一个Java类一起使用的大小的对象 - Creating an object with a size to use with another class Java 是否可以使用在另一个类的类中创建的对象的实例? 一世 - Is it possible to use instances of an object created in a class from another class? I 得到一个类的某个对象并在另一个类中使用 - get some object of one class and use in another class 如何获取另一个文件中一个类中已经存在的对象的数据,并将其用于Java中另一个文件中的另一个类? - How to get data of an object that already existed in one class in another file and use it to another class in another file in Java? 如何使用另一个类中的对象的特定变量? - How do I use a specific variable of an object that is in another class? 如何将对象中的 Arraylist 用于另一个类中的方法 - How can I use an Arraylist in an object for a method in another class 如何从Java中的另一个线程类更新和使用main()中的对象 - How to update and use an object in main() from another thread class in java 如何将 json 解析为 object 并在另一个 class 中使用 - How do parse json to object and use in another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM