繁体   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)

我正在查看其他人的代码。

我看到一个没有非静态字段的类,但其中的大多数方法都是非静态的,需要您使一个对象访问有效地静态运行的方法。

我可能不理解吗?

编辑

有人问例子。 这是更多信息。

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

一件特别奇怪的事情是,有两种删除文件的方法。 一个无论如何都将删除文件,而一个仅在文件为空时才将其删除。 前者是静态方法,而后者不是。 它们包含完全相同的代码,但稍后会先检查file.length是否为0。

另一个奇怪的类是进行加密的类-所有字段和方法都是静态的,但是它具有不执行任何操作的构造函数。 还有一个init()方法,它检查静态变量是否包含其自身的对象,如果不包含,则将其自身实例化到该字段中,而该字段从未真正使用过。 (看来,这是通过许多类完成的-初始化方法会在静态变量中检查自身的对象,如果没有实例化,则为实例)

 private static File keyfile;
 private static String KEYFILE = "enc.key";
 private static Scrambler sc; 

它具有加密和解密的方法以及一些用于处理密钥和文件的方法。

这对任何人有意义吗? 我只是不明白这些东西的目的吗? 还是看起来很奇怪?

对象不必有状态。 创建仅具有行为的类的实例是合法的用例。

为什么要创建一个实例呢? 因此,您可以创建一个并将其传递给它,例如,设想某种形式的计算器,该计算器遵循特定的接口,但是每个实例执行的计算方式不同。 接口的不同实现将执行不同的计算。

我经常创建带有非静态方法且没有成员的类。 它使我能够封装行为,而且我经常可以稍后添加成员,因为将来实现可能会需要这些成员(包括与非功能性相关的东西,例如工具),我通常不会使这些方法成为静态方法,因为这限制了我未来的灵活性。

您当然可以那样做。 您应该仔细查看实例方法在做什么。 如果它们都只对传入的参数和static final静态类常量进行操作,则完全可以。

如果是这样, 可以将所有这些方法设为静态。 那只是一个选择。 我不知道最初的开发者会如何证明其中之一。 也许你应该问他们。

使所有方法都是非静态的可以覆盖它们。 这使得在测试中使用此类非常容易,因为您可以使用模拟行为,而不是实际的实现,以模拟测试的行为。 在我的书中,静态方法是一种代码臭味,除非有充分的理由(例如,琐碎的实用程序方法),否则应避免使用。

同样,在将来的某个时候,您可能希望在某些情况下(例如以策略的形式)更改方法的行为。

就您的加密类而言,您可能希望为您的类提供一个加密类的实例,以处理加密/解密,但是可以在其他地方配置详细信息。 这样一来,您就可以更改算法并更轻松地测试自己的代码,而不必测试加密。

我再改一下这个问题,

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

我在下面引用了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 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, 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 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.

例如,假设您要创建多个Bicycle对象并为每个对象分配一个序列号,第一个对象以1开头。 该ID号对于每个对象都是唯一的,因此是一个实例变量。 同时,您需要一个字段来跟踪已创建了多少个Bicycle对象,以便您知道要分配给下一个对象的ID。 这样的字段与任何单个对象都不相关,而与整个类相关。

对于Bicycle示例,请参考Java Docs

暂无
暂无

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

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