Java's methods vs. functions

This question already has an answer here:

  • Difference between a method and a function 33 answers

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

    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

    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

    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

    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.


    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 ie 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

    Java is 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.

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

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


    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?

    Eg 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:

    // 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)
    
    链接地址: http://www.djcxy.com/p/17310.html

    上一篇: 职能只在非

    下一篇: Java的方法与功能