简体   繁体   English

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

[英]Java: What is the difference between subroutine and methods?

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). 不能在程序块外部访问局部变量(是的,在某些情况下,这是不正确的)。

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

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. 如果它是名为write.java的文件中名为write的类的无参数构造函数,则只有“正确”。 And even that violates the Sun Java coding standards. 甚至违反了Sun Java编码标准。

Subroutine is a term from functional/procedural languages like FORTRAN and COBOL. 子例程是功能语言或过程语言(例如FORTRAN和COBOL)的术语。 Those languages keep data and methods separate, with methods operating on the data they are given. 这些语言将数据和方法分开,方法对它们给出的数据进行操作。

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. 如果我是你,我会习惯“方法”。

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. 但是,如果您想更深入一点,那么我们将不得不进行IMO的汇编编程。 Subroutines in assembly, are in fact different from methods in two things: 实际上,汇编中的子例程与方法在两点上有所不同:

  1. They don't get parameters passed 他们没有传递参数
  2. They don't return anything 他们什么也没退

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: 如果可以满足任何要求,我将使用简单的子例程在程序集中向您发布一个o代码,以添加两个数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM