简体   繁体   English

使用所有非静态方法但没有非静态字段的类是否有意义? (或所有静态字段和方法以及构造函数)

[英]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)

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. 有一些方法可以执行一些操作,例如对列表中的文件进行排序,计数文件,复制文件,将文件移动到存档文件夹,删除超过特定时间的文件或创建文件(基本上将基本名称作为字符串,然后返回在文件末尾加上给定的基本名称和日期/时间。)9种非静态方法5种静态方法对于静态与非静态的方法,我没有看到特殊的押韵原因。

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. 它们包含完全相同的代码,但稍后会先检查file.length是否为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. 还有一个init()方法,它检查静态变量是否包含其自身的对象,如果不包含,则将其自身实例化到该字段中,而该字段从未真正使用过。 (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 eg 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. 它使我能够封装行为,而且我经常可以稍后添加成员,因为将来实现可能会需要这些成员(包括与非功能性相关的东西,例如工具),我通常不会使这些方法成为静态方法,因为这限制了我未来的灵活性。

You can certainly do it that way. 您当然可以那样做。 You should look carefully at what the instance methods are doing. 您应该仔细查看实例方法在做什么。 It's perfectly okay if they're all operating only on parameters passed in and static final static class constants. 如果它们都只对传入的参数和static final静态类常量进行操作,则完全可以。

If that's the case, it's possible to make all those methods static. 如果是这样, 可以将所有这些方法设为静态。 That's just a choice. 那只是一个选择。 I don't know how the original developers would justify either one. 我不知道最初的开发者会如何证明其中之一。 Maybe you should ask them. 也许你应该问他们。

Making all methods non-static allows you to override them. 使所有方法都是非静态的可以覆盖它们。 This makes it a lot easier to use this class in testing, because instead of the actual implementation you can use a mock that behaves as you want it for the tests. 这使得在测试中使用此类非常容易,因为您可以使用模拟行为,而不是实际的实现,以模拟测试的行为。 Static methods are, in my book, a code smell and should be avoided unless there's a good reason (eg quite trivial utility methods). 在我的书中,静态方法是一种代码臭味,除非有充分的理由(例如,琐碎的实用程序方法),否则应避免使用。

Also, at some point in the future you might want to change the behaviour of the methods in some situation, eg in the form of a strategy. 同样,在将来的某个时候,您可能希望在某些情况下(例如以策略的形式)更改方法的行为。

In the case of your encryption class, you might want to hand your class an instance of the encryption class to handle encrypting/decrypting, but be able to configure the details in some other place. 就您的加密类而言,您可能希望为您的类提供一个加密类的实例,以处理加密/解密,但是可以在其他地方配置详细信息。 That would allow you to change the algorithm and much more easily test your own code without also having to test the encryption. 这样一来,您就可以更改算法并更轻松地测试自己的代码,而不必测试加密。

Let me rephrase this question a bit, 我再改一下这个问题,

Even though methods are non-static why would one declare fields as static?

I have taken below quoting from Java Docs , 我在下面引用了Java Docs的话

Sometimes, you want to have variables that are common to all objects. This is Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. variable, but class variables can also be manipulated without creating an instance of the class.

For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. 例如,假设您要创建多个Bicycle对象并为每个对象分配一个序列号,第一个对象以1开头。 This ID number is unique to each object and is therefore an instance variable. 该ID号对于每个对象都是唯一的,因此是一个实例变量。 At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. 同时,您需要一个字段来跟踪已创建了多少个Bicycle对象,以便您知道要分配给下一个对象的ID。 Such a field is not related to any individual object, but to the class as a whole. 这样的字段与任何单个对象都不相关,而与整个类相关。

For Bicycle example, kindly refer the Java Docs . 对于Bicycle示例,请参考Java Docs

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM