Java:子程序和方法有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16760460/
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-10-31 23:55:23  来源:igfitidea点击:

Java: What is the difference between subroutine and methods?

javadefinitionsubroutine

提问by user2421111

What is the difference?

有什么区别?

public write(){
// dostuff
}

Is the code above a subroutine, a method, or a program block?

上面的代码是子程序、方法还是程序块?

I don't understand these terms because the meaning of them are all so similar?

我不明白这些术语,因为它们的含义都很相似?

So, if I was being very strict and pedantic, then which one would it be? I prefer calling them subroutines, is this STRICTLY correct?

所以,如果我非常严格和迂腐,那会是哪一种?我更喜欢称它们为子程序,这严格正确吗?

Programming language: JAVA

编程语言:JAVA

EDIT: Thanks, I understand now but what would be the safest way to call it? Block? I use multiple programming languages, would the term "block" suffice?

编辑:谢谢,我现在明白了,但最安全的称呼方式是什么?堵塞?我使用多种编程语言,术语“块”就足够了吗?

Can I call a function a block?

我可以将函数称为块吗?

So can I refer everything as a block? It will be easier for me, right?

那么我可以将所有内容都称为块吗?对我来说会更容易,对吧?

I won't call a block a function or subroutine, but I will call a function, methods and whatever as a block. Would that be fine, calling them as a block?

我不会将块称为函数或子例程,但我会将函数、方法和任何内容称为块。将它们称为块可以吗?

So, does this mean this is somewhat ACCURATE? A local variable is not accessed outside of the program block (yes, there are a few circumstances that this is not true).

那么,这是否意味着这有点准确?局部变量不会在程序块之外访问(是的,在某些情况下并非如此)。

回答by user207421

  1. A subroutine is something you can call that returns to you.
  2. A function is a subroutine that returns a a value.
  3. A method is a subroutine or function that you can call on an object in an OO language.
  1. 子程序是您可以调用并返回给您的东西。
  2. 函数是一个返回值的子程序。
  3. 方法是可以在面向对象语言中调用对象的子例程或函数。

回答by duffymo

None of the above, because it doesn't have a return value. It's only "correct" if it's a no-argument constructor for a class named write, in a file named write.java. And even that violates the Sun Java coding standards.

以上都不是,因为它没有返回值。如果它是名为 write.java 的文件中名为 write 的类的无参数构造函数,则它仅是“正确的”。甚至这也违反了 Sun Java 编码标准。

Subroutine is a term from functional/procedural languages like FORTRAN and COBOL. Those languages keep data and methods separate, with methods operating on the data they are given.

子程序是来自功能/过程语言(如 FORTRAN 和 COBOL)的术语。这些语言将数据和方法分开,方法对给定的数据进行操作。

Objects encapsulate data and methods into instances. Most object-oriented programmers would prefer method. Your "preference" for subroutine will mark you as quaint and out of step.

对象将数据和方法封装到实例中。大多数面向对象的程序员更喜欢方法。您对子程序的“偏好”会将您标记为古怪和不合时宜。

I don't hear "subroutine" much anymore. I'd get used to "method" if I were you.

我不再听到“子程序”了。如果我是你,我会习惯“方法”。

回答by Daniel Conde Marin

In the first place, what you have there is a constructor, and not a method, a proper method returning void(nothing) would be one like this(according to C# or Java syntax):

首先,您拥有的是一个构造函数,而不是一个方法,返回 void(nothing) 的正确方法应该是这样的(根据 C# 或 Java 语法):

public void write()
{}

Now, regarding to what you initially asked: What's the difference between subroutine and method?, Well, there is basically none. But if you want to go deeper, then we'll have to go to assembly programming IMO. Subroutines in assembly, are in fact different from methods in two things:

现在,关于您最初提出的问题:subroutine 和 method 之间有什么区别?,嗯,基本上没有。但是如果你想更深入,那么我们将不得不去 IMO 汇编编程。汇编中的子程序,实际上与方法有两点不同:

  1. They don't get parameters passed
  2. They don't return anything
  1. 他们没有得到传递的参数
  2. 他们不返回任何东西

And, you probably are asking yourself: Then how do I process anything at all, how this subroutine works? In assembly, here is where registers do their role, instead of passing a parameter to your subroutine, like you do with methods in every high level programming language, you have to save the value(parameter) you are going process before calling the subroutine, making sure that this value won't get affected before you reach the subroutine. In the same fashion, you'll keep the resulting value in another register for using it later. There are other ways to do this in assembly, commonly used too, by using the stack pushing and popping values from it, but I think I made my point clear enough. If serves for anything at all, I'll post you a piece o code in assembly using a simple subroutine to add two numbers:

而且,您可能会问自己:那么我如何处理任何事情,这个子程序是如何工作的?在汇编中,这里是寄存器发挥作用的地方,而不是将参数传递给您的子程序,就像您使用每种高级编程语言中的方法所做的那样,您必须在调用子程序之前保存要处理的值(参数),确保在到达子程序之前该值不会受到影响。以同样的方式,您将结果值保存在另一个寄存器中以备后用。在汇编中还有其他方法可以做到这一点,也很常用,通过使用来自它的堆栈推送和弹出值,但我认为我的观点已经足够清楚了。如果有任何用途,我将使用一个简单的子例程将两个数字相加,在汇编中向您发布一段代码:

org 00H

mov R3,#10                 ;save the first parameter
mov R4,#20                 ;save the second parameter

call Sum                   ;execute the subroutine

mov A,R3                   ;mov 30 to accumulator

Sum:                       ;subroutine declaration
add R3,R4
ret

end