只在类中调用的方法仍然是方法应该是静态的?

时间:2017-12-04 02:23:11

标签: static-methods

我理解使用静态方法的好处,在没有类实例的情况下调用功能,从而节省内存。

但是如果只在类中调用该方法呢?仍然,使用静态方法有什么好处吗?

Class Test {
    sayHelloWorld() {
        print "Hello" + getWorld()
    }


    // this method never will be called out side of the class
    // IDE say 'this method can be static'
    private getWorld() {
        return "world"
    }

}

1 个答案:

答案 0 :(得分:0)

您在2017年作为程序员的关注不应该是关于节省内存,而是更多关于将您的抽象放在正确的位置。如果您使用面向对象的语言,使用静态方法是一种糟糕的设计嗅觉嗅觉。它允许使用全局变量和函数进行C风格编程,并使代码通常不易测试。