静态与非静态方法2 [重复]

时间:2021-12-16 16:54:24

Possible Duplicate:
Static vs. non-static method

可能重复:静态与非静态方法

which one is better for a good design

哪一个更适合好的设计

or are there any difference? or is it just up to the developer?

或者有什么区别?还是仅仅取决于开发人员?

class Foo
{
    int x;

    void add(Foo* f1) //Method 1
    {
        x += f1->x;
    }

    static void add(Foo* f1, Foo* 2) //Method 2
    {
        f1->x = f1->x + f2->x;
    }

    static Foo* add(Foo* f1, Foo* 2) //Method 3
    {
        Foo* foo = new Foo();
        foo->x = f1->x + f2->x;
        return foo;
    }
}

4 个解决方案

#1


From an OO point of view, I think that the better way is:

从OO的角度来看,我认为更好的方法是:

class Foo
{
    int x;

    void add(Foo* f1) //Method 1
    {
        x += f1->x;
    }
}

a method should be linked to an object not to a class.

方法应该链接到不是类的对象。

C++ is Object oriented not Class oriented.

C ++是面向对象而非面向类。

In my opinion, using too many static method breaks the benefit of objects (polymorphsim, inheritance...)

在我看来,使用太多静态方法会破坏对象的好处(polymorphsim,inheritance ......)

#2


First and third options are good - which one to choose depends on your intentions. I wouldn't use second option - it doesn't reveal it's intention.

第一和第三种选择都很好 - 选择哪一种取决于你的意图。我不会使用第二种选择 - 它没有透露它的意图。

When you use first method, you clearly state, that addition will modify object. When a programmer sees:

当您使用第一种方法时,您明确指出,该添加将修改对象。当程序员看到:

Foo f1, f2;
f1.add(&f2);

He/she already knows, that f1 can/will be modified by this call.

他/她已经知道,f1可以/将被此调用修改。

When you use third method, you state, that none of passed objects will be modified, so when programmer sees:

当您使用第三种方法时,您声明不会修改任何传递的对象,因此当程序员看到时:

Foo f1, f2;
Foo *result;
result = Foo.add(&f1, &f2);

He/she knows, that f1 and f2 will NOT be modified (and you should use options that language gives you to enforce this).

他/她知道,f1和f2不会被修改(你应该使用语言为你强制执行的选项)。

As I wrote - choice between first and third method depends on your intentions. If you want to treat Foo as value object - that means that you can trust that once you set it's fields they will remain that way no matter where you pass your object, use third method. If you want to treat Foo as object which state can and will change, and you agree that passing a Foo object to a method can change it's state - use first option.

正如我所写 - 第一种和第三种方法之间的选择取决于你的意图。如果你想将Foo视为值对象 - 这意味着你可以相信,一旦你设置了它的字段,无论你在哪里传递你的对象,它们都将保持这种方式,使用第三种方法。如果你想将Foo视为状态可以改变的对象,并且你同意将Foo对象传递给方法可以改变它的状态 - 使用第一个选项。

However, when you use second option - programmer will not know whether you modify first, second, both or neither of arguments. Don't use this option.

但是,当您使用第二个选项时 - 程序员不会知道您是修改第一个,第二个,两个还是两个参数。不要使用此选项。

#3


The difference of the static-ness or non-static-ness of a method for a programmer is mostly a question of what design and behaviour you wish to accomplish. There is no absolute truth or an always correct answer for this.

程序员的方法的静态或非静态差异主要是你想要完成什么设计和行为的问题。对此没有绝对的真理或总是正确的答案。

The straightforward difference is that static methods are stateless whereas non-static methods are stateful. If it's a matter of instantiation, there are many designs that favour static methods, for example.

直截了当的区别是静态方法是无状态的,而非静态方法是有状态的。如果这是实例化的问题,那么有很多设计都支持静态方法。

But the correct answer for your design only you can achieve by inspecting thoroughly the responsibilities of the classes in your design.

但只有通过彻底检查设计中类的职责,才能获得正确的设计答案。

#4


If you can implement the function/method by using only the public features of the class, then you should make it a non-member function/method (a static method in a separate class in some languages, or an extension method in C# or VB.NET).

如果你可以只使用类的公共特性来实现函数/方法,那么你应该使它成为非成员函数/方法(在某些语言的单独类中的静态方法,或在C#或VB中的扩展方法) 。净)。

Static methods mixed into the same class as instance methods tend to be quite obscure and confusing. They can only see static data, so it's like mixing the definition of a singleton into the definition of something else. It's a lot clearer to separate them out.

与实例方法混合在同一个类中的静态方法往往相当模糊和混乱。它们只能看到静态数据,所以就像将单例的定义混合到其他东西的定义中一样。将它们分开是更清楚的。

#1


From an OO point of view, I think that the better way is:

从OO的角度来看,我认为更好的方法是:

class Foo
{
    int x;

    void add(Foo* f1) //Method 1
    {
        x += f1->x;
    }
}

a method should be linked to an object not to a class.

方法应该链接到不是类的对象。

C++ is Object oriented not Class oriented.

C ++是面向对象而非面向类。

In my opinion, using too many static method breaks the benefit of objects (polymorphsim, inheritance...)

在我看来,使用太多静态方法会破坏对象的好处(polymorphsim,inheritance ......)

#2


First and third options are good - which one to choose depends on your intentions. I wouldn't use second option - it doesn't reveal it's intention.

第一和第三种选择都很好 - 选择哪一种取决于你的意图。我不会使用第二种选择 - 它没有透露它的意图。

When you use first method, you clearly state, that addition will modify object. When a programmer sees:

当您使用第一种方法时,您明确指出,该添加将修改对象。当程序员看到:

Foo f1, f2;
f1.add(&f2);

He/she already knows, that f1 can/will be modified by this call.

他/她已经知道,f1可以/将被此调用修改。

When you use third method, you state, that none of passed objects will be modified, so when programmer sees:

当您使用第三种方法时,您声明不会修改任何传递的对象,因此当程序员看到时:

Foo f1, f2;
Foo *result;
result = Foo.add(&f1, &f2);

He/she knows, that f1 and f2 will NOT be modified (and you should use options that language gives you to enforce this).

他/她知道,f1和f2不会被修改(你应该使用语言为你强制执行的选项)。

As I wrote - choice between first and third method depends on your intentions. If you want to treat Foo as value object - that means that you can trust that once you set it's fields they will remain that way no matter where you pass your object, use third method. If you want to treat Foo as object which state can and will change, and you agree that passing a Foo object to a method can change it's state - use first option.

正如我所写 - 第一种和第三种方法之间的选择取决于你的意图。如果你想将Foo视为值对象 - 这意味着你可以相信,一旦你设置了它的字段,无论你在哪里传递你的对象,它们都将保持这种方式,使用第三种方法。如果你想将Foo视为状态可以改变的对象,并且你同意将Foo对象传递给方法可以改变它的状态 - 使用第一个选项。

However, when you use second option - programmer will not know whether you modify first, second, both or neither of arguments. Don't use this option.

但是,当您使用第二个选项时 - 程序员不会知道您是修改第一个,第二个,两个还是两个参数。不要使用此选项。

#3


The difference of the static-ness or non-static-ness of a method for a programmer is mostly a question of what design and behaviour you wish to accomplish. There is no absolute truth or an always correct answer for this.

程序员的方法的静态或非静态差异主要是你想要完成什么设计和行为的问题。对此没有绝对的真理或总是正确的答案。

The straightforward difference is that static methods are stateless whereas non-static methods are stateful. If it's a matter of instantiation, there are many designs that favour static methods, for example.

直截了当的区别是静态方法是无状态的,而非静态方法是有状态的。如果这是实例化的问题,那么有很多设计都支持静态方法。

But the correct answer for your design only you can achieve by inspecting thoroughly the responsibilities of the classes in your design.

但只有通过彻底检查设计中类的职责,才能获得正确的设计答案。

#4


If you can implement the function/method by using only the public features of the class, then you should make it a non-member function/method (a static method in a separate class in some languages, or an extension method in C# or VB.NET).

如果你可以只使用类的公共特性来实现函数/方法,那么你应该使它成为非成员函数/方法(在某些语言的单独类中的静态方法,或在C#或VB中的扩展方法) 。净)。

Static methods mixed into the same class as instance methods tend to be quite obscure and confusing. They can only see static data, so it's like mixing the definition of a singleton into the definition of something else. It's a lot clearer to separate them out.

与实例方法混合在同一个类中的静态方法往往相当模糊和混乱。它们只能看到静态数据,所以就像将单例的定义混合到其他东西的定义中一样。将它们分开是更清楚的。