Sie sind auf Seite 1von 2

method

In object-oriented programming, a method is a subroutine that is exclusively associated


either with a class (in which case it is called a class method or a static method) or with an
object (in which case it is an instance method). Like a subroutine in procedural
programming languages, a method usually consists of a sequence of programming
statements to perform an action, a set of input parameters to customize those actions, and
possibly an output value (called the return value) of some kind. Methods provide a
mechanism for accessing and manipulating the encapsulated data stored in an object.

The difference between procedures in general and an object's method is that the latter,
being associated with a particular object, may access or modify the data private to that
object in a way consistent with the intended behavior of the object. Consequently, rather
than thinking "a method is just a sequence of commands", a programmer using an object-
oriented language will consider a method to be "an object's way of providing a service"
(its "method of doing the job", hence the name); a method call is thus considered to be a
request to an object to perform some task.

Consequently, method calls are often modelled as a means of passing a message to an


object. Rather than directly performing an operation on an object, a message (most of the
time accompanied by parameters) is sent to the object telling it what it should do.

Types of methods
As mentioned above, instance methods are associated with an object, while class or
static methods are associated with a class. The object-oriented programming paradigm
intentionally favors methods for each and every means of access to the underlying data -
from its creation, initialization, retrieval and modification to its ultimate "destruction".

• An Instance method, in a typical implementation, is passed a hidden reference to


the object (whether a class or class instance) it belongs to, so that it can access the
data associated with it.
• For Class/static methods, this may or may not happen according to the language.
A typical example of a class method would be one that keeps count of the number
of created objects within a given class.

• An abstract method is a dummy code method which has no implementation. It is


often used as a placeholder to be overridden later by a subclass of or an object
prototyped from the one that implements the abstract method.
• An accessor method is a method that is usually small, simple and provides the
sole means for the state of an object to be accessed (retrieved) from other parts of
a program.
• An update, modifier, or mutator method, is an accessor method that changes the
state of an object. Objects that provide such methods are considered mutable
objects.
• A constructor method, supported by many languages, is called automatically
upon the creation of an instance of a class. Some languages have a special syntax
for constructors. E.g. In C++ the constructor method is split across two methods,
alloc and init, with the alloc method setting aside memory for an instance of
the class and the init method handling the bulk of initializing the instance. A call
to the new method invokes both the alloc and the init method for the class
instance.

• A destructor method (i.e. a special instance method that is called automatically


upon the destruction of an instance of a class), is implemented in some languages.

Add little description for the diagram ,,,,

Das könnte Ihnen auch gefallen