Content deleted Content added
TakuyaMurata (talk | contribs) mNo edit summary |
Added intro, added getBalance accessor example, fixed grammar |
||
Line 1:
A '''method''' is a function or sub-routine that is associated with a [[class (computer science)|class]] in object-oriented programming. Like a function in procedural languages, it may contain a set of program statements that perform an action, and (in most computer languages) can take a set of input arguments and can return some kind of result.
Whereas a [[C]] programmer might push a value onto a stack by calling:
An '''instance method''' is a method invoked with respect to an [[instance]] of a [[class (computer science)|class]]. Instance methods are often used to examine or modify the [[state]] of a particular [[object (computer science)|object]]. In [[Java programming language|Java]] and [[C Plus Plus|C++]], '''constructors''' are instance methods which have the same name as their class. In typical implementations, instance methods take a hidden reference to the object they belong to. ▼
stackPush(&myStack, value);
a [[C Plus Plus|C++]] programmer would write:
myStack.push(value);
the difference being that in C, the function is just a set of instructions; in C++, the push method is intimately associated with the stack; its implementation will typically be granted access to the stack's state which other parts of the program cannot reference.
The difference between a function and a method is that a method, being associated with a particular object, will access or modify some aspect of that object. Consequently, rather than thinking "a function is a grouped set of commands", an OO programmer will consider a method to be "this object's way of providing a service" (its "method of doing the job", hence the name); a method call should be considered to be a request to the object to perform a task. Method calls are often modelled as a means of passing a message to an object. Rather than pushing a value onto the stack, we send a value to stack, along with the message "push!", and the stack complies or raises an [[exception (computer science)|exception]].
▲An '''instance method''' is a method invoked with respect to an [[instance]] of a [[class (computer science)|class]]. Instance methods are often used to examine or modify the [[state]] of a particular [[object (computer science)|object]]. In [[Java programming language|Java]] and [[C Plus Plus|C++]], '''constructors''' are special instance methods
In contrast to instance methods, a '''class method''' (a.k.a '''static method''', '''shared method''') can be invoked without reference to a particular [[object (computer science)|object]]. These affect an entire [[Class (computer science)|class]], not merely a particular instance of the class. A typical example of a class method would be one that keeps count of the number of created objects within a given class.
Line 7 ⟶ 16:
An '''abstract method''' is a [[method]] which has no [[implementation]]. It is used to make a place-holder to be overriden later.
An '''accessor method''' is a kind of method that is usually small, simple and provides the
An accessor method that changes the state of an object is sometimes especially called '''mutator''' or '''update method'''. Objects with such a method are considered [[immutable objects|mutable objects]].
|