Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果类方法绝对不使用任何类字段,是否应该在所有情况下都将它们设为静态?
有例外吗?根据我的逻辑,如果一个类方法不使用它的任何字段,那么它必须是静态的。对?
如果一个方法在其包含的类中不使用任何实例变量,那么它不必是静态的,但它应该是。作为一般规则,如果方法不需要使用类实例化,则应将其作为静态方法提取到实用程序类/接口中。