Java 的方法与函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16223531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:51:50  来源:igfitidea点击:

Java's methods vs. functions

javafunctionmethods

提问by fpe

I just decided to integrate my MATLAB programming skills with some more consistent and rigorous Java coding. Therefore I hope it's not gonna be a too naive question.

我刚刚决定将我的 MATLAB 编程技能与一些更加一致和严格的 Java 编码相结合。因此,我希望这不会是一个太天真的问题。

I'd like knowing if there is any real reason why Java refers to functions as methodsand not as functions, as many other program languages do.

我想知道 Java 是否像许多其他程序语言那样将函数称为 asmethods而不是 as 的真正原因functions

Is it because of the inner OOPJava's nature compared with procedural languages such as C/C++? or are there any other important (or subtle) reasons?

是因为OOP与过程语言相比,Java 的内在本质C/C++吗?或者还有其他重要(或微妙)的原因吗?

Thanks in advance.

提前致谢。

采纳答案by Andrea Sindico

In my opinion this figure http://www.jot.fm/issues/issue_2008_03/article4/images/figure2.gif

在我看来这个数字http://www.jot.fm/issues/issue_2008_03/article4/images/figure2.gif

one, two and three dimensional method dispatch

一、二维、三维方法调度

from http://www.jot.fm/issues/issue_2008_03/article4/helps understanding one of the main differences between OO and procedural programming. Basically the idea is that

来自http://www.jot.fm/issues/issue_2008_03/article4/有助于理解面向对象和过程编程之间的主要区别之一。基本上这个想法是

Procedural programming provides only one dimension to associate a computational unit with a name. Here, procedure calls or names are directly mapped to procedure implementations. In Figure a calling m1 leaves no choice but the invocation of the only implementation of procedure m1

过程编程仅提供一个维度来将计算单元与名称相关联。在这里,过程调用或名称直接映射到过程实现。在图中,调用 m1 除了调用过程 m1 的唯一实现之外别无选择

while

尽管

Object-oriented programming adds another dimension for name resolution to that of procedural programming . In addition to the method or procedure name, message dispatch takes the message receiver into consideration when looking up a method. In Figure 2b we see two implementations of method m1. The selection of the appropriate method not only depends on the the message name m1, but also the receiver of the actual message, here Ry

面向对象的编程为过程编程的名称解析增加了另一个维度。除了方法或过程名称之外,消息调度在查找方法时还会考虑消息接收者。在图 2b 中,我们看到方法 m1 的两个实现。选择合适的方法不仅取决于消息名称m1,还取决于实际消息的接收者,这里是Ry

the third section of the figure (c) refers to subject oriented programming, in which the behavior of an object (the called method) does not only depend on the object status but, also, on the subjects which is invoking (or observing) it. However this is actually out of the scope of your question.

图(c)的第三部分指的是面向对象的编程,其中对象(被调用的方法)的行为不仅取决于对象状态,还取决于调用(或观察)它的对象. 但是,这实际上超出了您的问题范围。

回答by Captain Skyhawk

They're the same. C++ typically calls them functions. Java typically refers to them as methods.

他们是一样的。C++ 通常将它们称为函数。Java 通常将它们称为方法。

Methods are typically associated with a class.

方法通常与类相关联。

You'll occasionally hear "class function" too, which is just a method.

您偶尔也会听到“类函数”,这只是一种方法。

It doesn't matter, people will know what you're talking about if you call them either.

没关系,如果你给他们打电话,人们也会知道你在说什么。

回答by gprathour

Well there is a little difference between a method and a function.

方法和函数之间有一点区别。

A function is just a code that you can call anytime by its name and you can pass arguments also known as parameters to it and you can also get the result from any function i.e. return value of the function.

函数只是一个代码,您可以随时通过其名称调用它,您可以将参数也称为参数传递给它,您还可以从任何函数中获取结果,即函数的返回值。

But a method is a code that is called by its name but it is associated to any object. You can pass parameters to methods also and you can also get some return value from methods but thing is they will always be associated with some objects.

但是方法是一种代码,它以其名称调用,但它与任何对象相关联。您也可以将参数传递给方法,也可以从方法中获取一些返回值,但问题是它们将始终与某些对象相关联。

EDITED

已编辑

Javais object oriented, you cannot have Java code to run without classes in most cases however in C++ you can get your code run without classes. So in Java there will be classes and code will be written in classes so they are called methods instead of functions, as they will be associated with objects.

Java是面向对象的,在大多数情况下,Java 代码不能在没有类的情况下运行,但是在 C++ 中,您可以在没有类的情况下运行代码。因此,在 Java 中会有类,代码将编写在类中,因此它们被称为方法而不是函数,因为它们将与对象相关联。

But in C++ you can have some function that can be called by passing values explicitly.

但是在 C++ 中,您可以拥有一些可以通过显式传递值来调用的函数。

In simple terms you can say, a method is a function that is related to an object.

简单来说,方法是与对象相关的函数。

回答by Brian

Can't help thinking a lot of unnecessary drama in this one. "methods" is just a name surely, that Java happens to use, for subroutines which may or may not require parameters, and may or may not return a value?

不禁在这一部想了很多不必要的戏剧。“方法”肯定只是一个名称,Java 碰巧使用,用于可能需要也可能不需要参数并且可能会或可能不会返回值的子例程?

E.g. valid "methods" might be as follows, without getting into OO purity, canonical definitions of "functions", etc; both of the below may or may not use an object's current "state" (instance variable values) in their execution too:

例如,有效的“方法”可能如下,而不涉及 OO 纯度、“函数”的规范定义等;以下两个都可能会或可能不会在执行中使用对象的当前“状态”(实例变量值):

// kind of a function, returns a value
public int calculateStuff(int param1)

// more of a procedure, presumably just "does stuff", returns nothing
public void doStuff(int param1)