Are functions only in non

This question already has an answer here:

  • Difference between a method and a function 33 answers

  • Are functions found only in non-object-oriented languages?

    No. There are object-oriented languages that have functions. C#, for example, is an object-oriented language but it has anonymous functions.

    What are the named procedures which are members of a type typically called in object-oriented languages like Java or C#?

    Typically they are properly called methods, though this differs from language to language. In Java or C# I would say "method".

    In Visual Basic, for example, the distiction is made between functions and subroutines on the basis of whether or not they return a value, not on the basis of whether they are associated with a type container.

    JavaScript, an object-oriented language which uses prototype inheritance rather than class inheritance, typically refers to all of the above as "functions".

    Do people frequently refer to methods as functions when speaking casually about Java or C#?

    Yes. Were I writing documentation or a book or a scholarly article then I would be careful to make the distinction. In commonplace parlance though everyone reasonably conversant with the art of computer programming would understand "function" and "method" to be roughly synonyms. I would not have rejected your answer.


    Any answer which limits this to a specific language is inherently flawed. In addition you must also deal effectively with static methods and subroutines.

    Computer science began with the term ' subroutine '. Small sections of repeatable code which could be executed arbitrarily to perform a common action. Examples are found in early programming languages such as BASIC.

    Functions were the evolution of subroutines. They take arguments and may or may not return a value. They take some concepts from maths - input, translated to a given output.

    With objects we need to be able to call actions on objects and we do this be exposing methods . Like functions they take arguments and may or may not return a value.

    Static methods are designed to act on all possible objects of a class.

    The problem is that, pure object-orientated programming leaves no scope for the definition of functions (or indeed subroutines). And languages that evolve to become object orientated often retain syntax from functions to implement methods.

    In Java we resort to using 'Utility' classes to provide functions as public static methods. The Math class in JavaScript is another example of this.

    In PHP we tolerate the use of the word function to define methods.

    In C++ we see both functions and methods, neither demarcated. Indeed, C++ makes no reference to methods, calling them member functions.


    A function is not bound to a class.

    A function is something like doStuff(); .

    A method is like someThing.doStuff(); or SomeClass.doStuff(); .

    In Java, there is no such thing as a function. They are all methods. ie

        class Test {
    
            public static void doSomething() {...}
            public void otherThing() {...}
    
            public static void main(String[] args) {
                doSomething(); //implied Test.doSomething();
            }
    
            public Test() {
                otherThing(); //implied this.otherThing();
            }
        }
    
    链接地址: http://www.djcxy.com/p/17312.html

    上一篇: 为什么函数在java中被称为方法?

    下一篇: 职能只在非