我正在用 Java 制作一个简单的电子邮件应用程序。我有一个 Account
类。每个Account
类都有一个username
和password
属性。我希望能够从另一个类创建 Account
类的新实例。我怎样才能做到这一点?看起来应该是非常简单的事情,但我似乎无法弄清楚。
最佳答案
如果Account
有一个复制构造函数,那就很简单了:
Account newAccount = new Account(otherAccount);
如果没有,你可以做类似的事情
String username = otherAccount.getUserName();
String password = otherAccount.getPassword();
Account newAccount = new Account(username, password);
显然,我只需要编写一些方法名称和其他内容,但您明白了。
关于java - 如何在 Java 中创建对象的新实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5841509/