[英]how do I pass information from one class to another
I am creating a program which allows the user to input details of customers.我正在创建一个程序,允许用户输入客户的详细信息。 When they have saved each customers record there is the choice to add additional information.当他们保存每个客户记录后,可以选择添加其他信息。 I am having trouble getting the the name of the saved file in my append class.我无法在 append class 中获取已保存文件的名称。 I need the filename so I can then save the additional information to the same file already created for the customer.我需要文件名,以便我可以将附加信息保存到已为客户创建的同一文件中。 How do I pass the file name from one file to another.如何将文件名从一个文件传递到另一个文件。
File FName = fileChooser.getSelectedFile();
String name = FName.getName();
public String getname() { return name; }
This code is in my customer class how do I get this information in my append class??此代码在我的客户 class 中,如何在我的 append class 中获取此信息?
Possibly something like this:可能是这样的:
Customer customer = new Customer();
// do some stuff with your customer object, including initiating the File and saving its name to a String field called name
Append append = new Append();
append.foo(customer.getName()); // passes the name of the file to the foo method of class Append
This assumes that you'll only want the name of the file in that one method (though you could save it to a field as part of method foo()
).这假设您只需要该方法中的文件名(尽管您可以将其作为方法foo()
的一部分保存到字段中)。 You'd need to implement a method foo(String name)
in class Append.您需要在 class Append 中实现方法foo(String name)
。
Another option would be to pass it as a constructor of Append:另一种选择是将其作为 Append 的构造函数传递:
Append append = new Append(customer.getName());
append.foo();
For this, you'd need to implement a constructor Append(String name)
in class Append.为此,您需要在 class Append 中实现构造函数Append(String name)
。
There are a couple of ways to do this depending on what exactly it is you are trying to do.有几种方法可以做到这一点,具体取决于您要尝试做什么。
Your question is not entirely clear to me, but here's a problems you might run into based on your description:您的问题对我来说并不完全清楚,但根据您的描述,您可能会遇到以下问题:
I don't really understand why you'd need an Append class.我真的不明白你为什么需要 Append class。 Personally, I'd handle appending additional info inside the Customer class.就个人而言,我会在客户 class 中处理附加信息。 I'd do it so that every time a value is added or changed, the info is saved back into the file like:我会这样做,以便每次添加或更改值时,信息都会保存回文件中,例如:
class Customer {
public void setForename(String forename) {
this.forename = forename;
save();
}
public void setSurname(String surname) {
this.surname = surname;
save();
}
public void save() {
// clear file
// add new content
String fileContents = "forename="+forname+"&surname="+surname;
// save to file
}
Maybe I'm not understanding your needs correctly though...也许我没有正确理解您的需求......
In addition to DeadPassive's answer of associating the additional information with the Customer object:除了 DeadPassive 将附加信息与客户 object 相关联的回答:
The saving of the Customer data does not belong in the Customer Class.客户数据的保存不属于客户 Class。 The logic of persisting the data belongs in a seperate layer than the code that deals with manipulating the problem domain.与处理问题域的代码相比,持久化数据的逻辑属于单独的层。 A controller or service class seems like a more appropriate place to put the persistance logic. controller 或服务 class 似乎是放置持久性逻辑的更合适的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.