[英]Store a directory as a File object or a String?
In my application I have a class called MyObject
. 在我的应用程序中,我有一个名为
MyObject
的类。 This class must have a member that represents a directory (the object is going to write in this directory). 此类必须具有代表目录的成员(对象将在此目录中写入)。
Should I use a String
or a File
object for this member? 我应该为此成员使用
String
还是File
对象?
Use a File
object. 使用
File
对象。 It's always best to choose an object type that most closely mirrors the usage of the data. 始终最好选择最能反映数据使用情况的对象类型。
Incidentally, you should never call your object MyObject
. 顺便说一句,永远不要调用对象
MyObject
。 Pick a more meaningful name that describes the purpose of your object. 选择一个更有意义的名称来描述对象的用途。
I don't think it matters much. 我认为这没什么大不了的。 Probably most clean would be to have a final File field, assuming you need to create a File object anyway.
假定您仍然需要创建File对象,那么最干净的方法可能是拥有一个最终的File字段。
Edit inspired by comment: What matters more is, if you have a getter for this member, should this getter return File
or String
or perhaps even URL
or URI
? 编辑受评论启发:更重要的是,如果您对此成员有一个getter,则该getter是否应返回
File
或String
甚至URL
或URI
? That affects public API of the class, and that should be given some serious thought. 这会影响该类的公共API,应该认真考虑一下。 And it may be easiest to just not provide getter until you need one (unless it is a library which you need to make "complete"), but instead just have a meaningful
toString()
override for debugging purposes etc. 而且,最简单的方法是在需要使用getter之前不提供getter(除非它是一个需要“完成”的库),而只是为了调试等目的而有意义地重写
toString()
,这可能是最容易的。
files are terrible to test. 文件很难测试。 try TDD - it will improve your design.
尝试TDD-它将改善您的设计。 i'm not aware of any file abstractions libraries but maybe you should consider looking for one.
我不知道任何文件抽象库,但也许您应该考虑寻找一个。 or at least try to split you business code from file handling code as much as possible
或至少尝试将您的业务代码与文件处理代码尽可能地分开
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.