我在看别人的代码。
我看到一个没有非静态字段的类,但是其中大多数方法都是非静态的,要求你创建一个对象来访问有效静态操作的方法。
是否有可能的原因,我只是不理解?
编辑
有人问了一些例子。 这是一些更多的信息。
例如,有一个文件管理器类。 唯一的字段是静态的,是比较器。 有一些方法可以执行以下操作:排序列表中的文件,计算文件,复制文件,将文件移动到存档文件夹,删除早于特定时间的文件,或创建文件(基本上将基本名称作为字符串,并返回具有给定基本名称的文件和最后添加的日期/时间。)9非静态方法5静态方法我没有看到静态与非静态方法的特定押韵原因。
一个特别奇怪的是,有两种方法可以删除文件。 一个删除文件,无论什么,一个只删除它,如果它是空的。 前者是静态方法,后者则不是。 它们包含完全相同的代码,但后面的第一次检查是否为file.length为0。
另一个奇怪的是一个加密的类 - 所有字段和方法都是静态的,但它有一个什么都不做的构造函数。 还有一个init()方法,用于检查静态变量是否包含自身的对象,如果没有,则将其自身的对象实例化为该字段,然后从不实际使用。 (似乎这是通过很多类完成的 - 在静态变量中检查自身对象的init方法,如果没有实例化自身)
private static File keyfile; private static String KEYFILE = "enc.key"; private static Scrambler sc;它有加密和解密的方法以及处理密钥和文件的一些方法。
这对任何人都有意义吗? 我只是不明白这个东西的目的吗? 还是看起来很怪异?
I am looking at other peoples' code.
I see a class with no non-static fields but in which most of the methods are non-static, requiring you to make an object to access methods that effectively operate statically.
Is there a possible reason for this, that I am just not understanding?
EDIT
Someone asked for examples. Here is some more info.
For instance there is a file manager class. The only fields are static and are Comparators. There are some methods to do things like sort files in a list, count files, copy files, move files to an archive folder, delete files older than a certain time, or create files (basically take a base name as string, and return a File with given base name and date/time tacked on the end.) 9 non-static methods 5 static methods I don't see a particular rhyme reason for the ones that are static vs non.
One particularly odd thing is that there are two methods for removing files. One that removes a file no matter what, and one that only removes it if it is empty. The former is a static method while the latter is not. They contain the same exact code except the later first checks if the file.length is 0.
Another odd one is a class that does encryption - all fields and methods are static but it has a constructor that does nothing. And an init() method that checks if a static variable contains an object of itself and if not instantiates an object of itself into that field that is then never actually used. (It seems this is done with a lot of classes - init methods that check for an object of itself in a static variable and if not instantiate itself)
private static File keyfile; private static String KEYFILE = "enc.key"; private static Scrambler sc;It has methods to encrypt and decrypt and some methods for dealing with key and file.
Does this make sense to anyone? Am I just not understanding the purpose for this stuff? Or does it seem weird?
最满意答案
对象不必具有状态。 创建仅具有行为的类的实例是合法的用例。
为什么要创建一个实例呢? 因此,您可以创建一个并传递它,例如想象某种形式的计算器,它遵循特定的接口,但每个实例执行不同的计算。 接口的不同实现将以不同方式执行计算。
我经常用非静态方法创建类,没有成员。 它允许我封装行为,我可以经常添加成员,因为实现可能在将来要求(包括非功能相关的东西,如仪器)我通常不会使这些方法静态,因为这限制了我未来的灵活性。
Objects don't have to have state. It's a legitimate use case to create an instance of a class with only behaviour.
Why bother to create an instance ? So you can create one and pass it around e.g. imagine some form of calculator which adheres to a particular interface but each instance performs a calculation differently. Different implements of the interface would perform calculations differently.
I quite often create classes with non-static methods and no members. It allows me to encapsulate behaviour, and I can often add members later as the implementation may demand in the future (including non-functionality related stuff such as instrumentation) I don't normally make these methods static since that restricts my future flexibility.
是否有一个类具有所有非静态方法但没有非静态字段?(Is there a point to having a class with all non-static methods but no non-static fields? (or all static fields and methods along with a constructor))我在看别人的代码。
我看到一个没有非静态字段的类,但是其中大多数方法都是非静态的,要求你创建一个对象来访问有效静态操作的方法。
是否有可能的原因,我只是不理解?
编辑
有人问了一些例子。 这是一些更多的信息。
例如,有一个文件管理器类。 唯一的字段是静态的,是比较器。 有一些方法可以执行以下操作:排序列表中的文件,计算文件,复制文件,将文件移动到存档文件夹,删除早于特定时间的文件,或创建文件(基本上将基本名称作为字符串,并返回具有给定基本名称的文件和最后添加的日期/时间。)9非静态方法5静态方法我没有看到静态与非静态方法的特定押韵原因。
一个特别奇怪的是,有两种方法可以删除文件。 一个删除文件,无论什么,一个只删除它,如果它是空的。 前者是静态方法,后者则不是。 它们包含完全相同的代码,但后面的第一次检查是否为file.length为0。
另一个奇怪的是一个加密的类 - 所有字段和方法都是静态的,但它有一个什么都不做的构造函数。 还有一个init()方法,用于检查静态变量是否包含自身的对象,如果没有,则将其自身的对象实例化为该字段,然后从不实际使用。 (似乎这是通过很多类完成的 - 在静态变量中检查自身对象的init方法,如果没有实例化自身)
private static File keyfile; private static String KEYFILE = "enc.key"; private static Scrambler sc;它有加密和解密的方法以及处理密钥和文件的一些方法。
这对任何人都有意义吗? 我只是不明白这个东西的目的吗? 还是看起来很怪异?
I am looking at other peoples' code.
I see a class with no non-static fields but in which most of the methods are non-static, requiring you to make an object to access methods that effectively operate statically.
Is there a possible reason for this, that I am just not understanding?
EDIT
Someone asked for examples. Here is some more info.
For instance there is a file manager class. The only fields are static and are Comparators. There are some methods to do things like sort files in a list, count files, copy files, move files to an archive folder, delete files older than a certain time, or create files (basically take a base name as string, and return a File with given base name and date/time tacked on the end.) 9 non-static methods 5 static methods I don't see a particular rhyme reason for the ones that are static vs non.
One particularly odd thing is that there are two methods for removing files. One that removes a file no matter what, and one that only removes it if it is empty. The former is a static method while the latter is not. They contain the same exact code except the later first checks if the file.length is 0.
Another odd one is a class that does encryption - all fields and methods are static but it has a constructor that does nothing. And an init() method that checks if a static variable contains an object of itself and if not instantiates an object of itself into that field that is then never actually used. (It seems this is done with a lot of classes - init methods that check for an object of itself in a static variable and if not instantiate itself)
private static File keyfile; private static String KEYFILE = "enc.key"; private static Scrambler sc;It has methods to encrypt and decrypt and some methods for dealing with key and file.
Does this make sense to anyone? Am I just not understanding the purpose for this stuff? Or does it seem weird?
最满意答案
对象不必具有状态。 创建仅具有行为的类的实例是合法的用例。
为什么要创建一个实例呢? 因此,您可以创建一个并传递它,例如想象某种形式的计算器,它遵循特定的接口,但每个实例执行不同的计算。 接口的不同实现将以不同方式执行计算。
我经常用非静态方法创建类,没有成员。 它允许我封装行为,我可以经常添加成员,因为实现可能在将来要求(包括非功能相关的东西,如仪器)我通常不会使这些方法静态,因为这限制了我未来的灵活性。
Objects don't have to have state. It's a legitimate use case to create an instance of a class with only behaviour.
Why bother to create an instance ? So you can create one and pass it around e.g. imagine some form of calculator which adheres to a particular interface but each instance performs a calculation differently. Different implements of the interface would perform calculations differently.
I quite often create classes with non-static methods and no members. It allows me to encapsulate behaviour, and I can often add members later as the implementation may demand in the future (including non-functionality related stuff such as instrumentation) I don't normally make these methods static since that restricts my future flexibility.
发布评论