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.
如果我们可以确保实例是单例,那么静态方法总是可以转换为实例方法吗?那正确吗?
当然,标记为的方法static可以转换为实例方法。标记为static的方法必须是不使用任何特定于实例的状态信息的方法(除非实例作为参数传入)。它们可以被重写为实例方法,并且仍然不依赖于特定于实例的状态信息(或this改为使用)。
static
this
您当然可以这样做,但您可能会破坏调用静态方法的现有代码。特别是如果该方法是公开可见的。