函数有点像静态方法吗?

时间:2022-02-01 16:54:47

I'm a java programmer and am trying to understand the difference between a method (java methods) and a function (such as in c++). I used to think that they are the same, just different naming conventions for different programming languages. But now that I know they are not, I am having trouble understanding the difference.

我是一名java程序员,正在尝试理解方法(java方法)和函数(例如c ++)之间的区别。我曾经认为它们是相同的,只是不同编程语言的不同命名约定。但是现在我知道他们不是,我无法理解差异。

I know that a method relates to an instance of a class and has access to class data (member variables), while a function does not (?). So is a function kind of like a static method?

我知道一个方法涉及一个类的实例,并且可以访问类数据(成员变量),而一个函数不能(?)。那么函数有点像静态方法吗?

See here for explanations I read which led me to think this.

在这里看到我读到的解释,这让我想到了这一点。

5 个解决方案

#1


4  

A function is simply a generic name for a portion of code within a program. The word "method" is a synonym for function. So are "subroutines" and "procedures," etc.

函数只是程序中部分代码的通用名称。 “方法”一词是功能的同义词。 “子程序”和“程序”等也是如此。

Java and C++ functions are for the most part exactly the same thing.

Java和C ++函数在很大程度上完全相同。

The word "method" tends to be used for subroutines associated with an instance, while "function" tends to be used for those that are global/static.

“方法”一词倾向于用于与实例相关联的子程序,而“函数”倾向于用于全局/静态的子程序。

But even then, "methods" are generated by the compiler as though they were "functions."

但即使这样,编译器也会生成“方法”,就好像它们是“函数”一样。

Consider this C++ code:

考虑这个C ++代码:

class Foo
{
public:
    void DoFoo(int param)
    {
        printf("%d, %d\n", param, member);
    }
private:
    int member;
};

int main()
{
    Foo f;
    f.DoFoo(42);
    return 0;
}

The compiler generates code to something equivalent to this:

编译器生成的代码与此相当:

struct Foo
{
    int member;
};

void Foo_DoFoo(Foo* this, int param)
{
    printf("%d, %d\n", param, this->member);
}

int main()
{
    Foo f;
    Foo_DoFoo(&f, 42);
    return 0;
}

So the distinction between "method" and "function" is merely a convention.

因此,“方法”和“功能”之间的区别仅仅是一种惯例。

#2


2  

This is strictly a vocabulary difference. Some consider a method an operation that belongs to an object or a class, and a function an operation that doesn't. Others, like the C++ crowd, call them both functions but refer to free functions or non-member functions when the function doesn't belong to a class or an object. I personally use the two interchangeably.

这严格来说是词汇差异。有些人认为方法是属于对象或类的操作,而函数则是不属于某个操作的操作。其他人,比如C ++人群,将它们称为两个函数,但当函数不属于类或对象时,引用*函数或非成员函数。我个人可以互换地使用这两个。

All in all, in the C++ jargon when you wish to refer to specific kinds of functions (non-members, members, returning no value, ...) you add an adjective to the noun function: a friend function, a void function, a member function and so on.

总而言之,当您希望引用特定类型的函数(非成员,成员,不返回值,...)时,在C ++术语中,您可以为名词函数添加一个形容词:友元函数,void函数,成员函数等。

#3


1  

In C all "functions" are "top level" in the sense that they were not associated with a type. If they were in your scope (e.g., via an include), you could refer to them and they could be linked.

在C中,所有“函数”都是“*”,因为它们与类型无关。如果它们在您的范围内(例如,通过包含),您可以引用它们并且它们可以链接。

In C++ you can create classes and place methods in them. Methods marked as static are invoked via a particular class but are not associated with an instance of the class. In that sense they are like functions. However, they are allowed some privileges associated with the class (e.g., they can be made private and can access private static members). However, you can still use C-style functions, for example for library functions.

在C ++中,您可以创建类并在其中放置方法。标记为static的方法通过特定类调用,但不与类的实例关联。从这个意义上讲,它们就像是功能。但是,它们被允许与该类相关联的一些特权(例如,它们可以被设为私有并且可以访问私有静态成员)。但是,您仍然可以使用C风格的函数,例如库函数。

In Java every method is associated with a class, so there are static methods but no C-style functions.

在Java中,每个方法都与一个类相关联,因此存在静态方法但没有C风格的函数。

#4


0  

Lets put it really simple a function is the same as a Java static method, however a function does not require a class to exist, which turns it as Uri says in a top level piece of code. One of the advantages of this is that it won't require an object instantiation to be called.

让我们说它非常简单,一个函数与Java静态方法相同,但函数不需要存在类,这就像Uri在*代码段中所说的那样。这样做的一个优点是它不需要调用对象实例化。

#5


0  

Is a function kind of like a static method?

函数有点像静态方法吗?

Kind of. But I'd rather say that a static method is like a function that has been enslaved and shackled to an object. For an elaboration of this point of view see Execution in the Kingdom of Nouns.

有点。但我宁愿说静态方法就像一个被奴役和束缚在一个物体上的函数。有关这一观点的详细说明,请参阅名词王国中的执行。

#1


4  

A function is simply a generic name for a portion of code within a program. The word "method" is a synonym for function. So are "subroutines" and "procedures," etc.

函数只是程序中部分代码的通用名称。 “方法”一词是功能的同义词。 “子程序”和“程序”等也是如此。

Java and C++ functions are for the most part exactly the same thing.

Java和C ++函数在很大程度上完全相同。

The word "method" tends to be used for subroutines associated with an instance, while "function" tends to be used for those that are global/static.

“方法”一词倾向于用于与实例相关联的子程序,而“函数”倾向于用于全局/静态的子程序。

But even then, "methods" are generated by the compiler as though they were "functions."

但即使这样,编译器也会生成“方法”,就好像它们是“函数”一样。

Consider this C++ code:

考虑这个C ++代码:

class Foo
{
public:
    void DoFoo(int param)
    {
        printf("%d, %d\n", param, member);
    }
private:
    int member;
};

int main()
{
    Foo f;
    f.DoFoo(42);
    return 0;
}

The compiler generates code to something equivalent to this:

编译器生成的代码与此相当:

struct Foo
{
    int member;
};

void Foo_DoFoo(Foo* this, int param)
{
    printf("%d, %d\n", param, this->member);
}

int main()
{
    Foo f;
    Foo_DoFoo(&f, 42);
    return 0;
}

So the distinction between "method" and "function" is merely a convention.

因此,“方法”和“功能”之间的区别仅仅是一种惯例。

#2


2  

This is strictly a vocabulary difference. Some consider a method an operation that belongs to an object or a class, and a function an operation that doesn't. Others, like the C++ crowd, call them both functions but refer to free functions or non-member functions when the function doesn't belong to a class or an object. I personally use the two interchangeably.

这严格来说是词汇差异。有些人认为方法是属于对象或类的操作,而函数则是不属于某个操作的操作。其他人,比如C ++人群,将它们称为两个函数,但当函数不属于类或对象时,引用*函数或非成员函数。我个人可以互换地使用这两个。

All in all, in the C++ jargon when you wish to refer to specific kinds of functions (non-members, members, returning no value, ...) you add an adjective to the noun function: a friend function, a void function, a member function and so on.

总而言之,当您希望引用特定类型的函数(非成员,成员,不返回值,...)时,在C ++术语中,您可以为名词函数添加一个形容词:友元函数,void函数,成员函数等。

#3


1  

In C all "functions" are "top level" in the sense that they were not associated with a type. If they were in your scope (e.g., via an include), you could refer to them and they could be linked.

在C中,所有“函数”都是“*”,因为它们与类型无关。如果它们在您的范围内(例如,通过包含),您可以引用它们并且它们可以链接。

In C++ you can create classes and place methods in them. Methods marked as static are invoked via a particular class but are not associated with an instance of the class. In that sense they are like functions. However, they are allowed some privileges associated with the class (e.g., they can be made private and can access private static members). However, you can still use C-style functions, for example for library functions.

在C ++中,您可以创建类并在其中放置方法。标记为static的方法通过特定类调用,但不与类的实例关联。从这个意义上讲,它们就像是功能。但是,它们被允许与该类相关联的一些特权(例如,它们可以被设为私有并且可以访问私有静态成员)。但是,您仍然可以使用C风格的函数,例如库函数。

In Java every method is associated with a class, so there are static methods but no C-style functions.

在Java中,每个方法都与一个类相关联,因此存在静态方法但没有C风格的函数。

#4


0  

Lets put it really simple a function is the same as a Java static method, however a function does not require a class to exist, which turns it as Uri says in a top level piece of code. One of the advantages of this is that it won't require an object instantiation to be called.

让我们说它非常简单,一个函数与Java静态方法相同,但函数不需要存在类,这就像Uri在*代码段中所说的那样。这样做的一个优点是它不需要调用对象实例化。

#5


0  

Is a function kind of like a static method?

函数有点像静态方法吗?

Kind of. But I'd rather say that a static method is like a function that has been enslaved and shackled to an object. For an elaboration of this point of view see Execution in the Kingdom of Nouns.

有点。但我宁愿说静态方法就像一个被奴役和束缚在一个物体上的函数。有关这一观点的详细说明,请参阅名词王国中的执行。