Method (programming)
Methods ( English method or member function ) are subroutines in object-oriented programming in the form of functions or procedures that describe and implement the behavior of objects . Objects can connect to one another via the methods of the object.
definition
The core of any object-oriented programming is the object . In general one can say that objects contain attributes and methods. Attributes are only variables and constants that can accept values and thus describe the static nature of the object. In contrast, there are the methods that characterize the entire dynamic behavior of the object or a class . They contain the algorithmic essence of the object. In object-oriented programming, methods are part of the definition of a class.
Method call
In the definition of a method, one or more formal parameters can be specified with which the method can be called. There are also methods without parameters . A formal parameter is the name of a variable and is also called a transfer value. In many programming languages, it is also necessary to declare the data types of these variables. These data types can be elementary data types or classes , i.e. object types. The method is executed when it is addressed via the method name and appropriate current parameters . The instructions in the method body are then executed with these transfer values. These instructions are executed top to bottom and left to right.
The name of the method as well as the number, type and order of the assignment-compatible data types of the parameters and, if available, the return type define the signature of the method.
example
The following Example in the programming language C # shows the methods berechneEinnahmen(...)
, gibPartei()
, wähleVorsitzender(...)
and gibAnzahlDerAbgeordneten(...)
, of the classes Partei
, Abgeordneter
and Parlament
can be defined. The parameters are declared in brackets after the name of the method. The data type always comes before the name of the variable . The return type comes before the name of the method. The return type is void
specified for methods without a return value .
class Partei
{
List<Person> mitglieder;
double mitgliedsbeitrag;
// Methode mit zwei Parametern und mit Rückgabewert
double berechneEinnahmen(double spenden, double sonstigeEinnahmen)
{
return mitglieder.Count * mitgliedsbeitrag + spenden + sonstigeEinnahmen;
}
}
class Abgeordneter
{
Partei partei;
// Methode ohne Parameter und mit Rückgabewert
Partei gibPartei()
{
return partei;
}
}
class Parlament
{
List<Abgeordneter> abgeordnete = new List<Abgeordneter>();
Abgeordneter vorsitzender;
int maximalGroesse;
// Methode mit einem Parameter und ohne Rückgabewert
void wähleVorsitzender(Abgeordneter abgeordneter)
{
vorsitzender = abgeordneter;
}
// Methode mit einem Parameter und mit Rückgabewert
int gibAnzahlDerAbgeordneten(Partei partei)
{
int anzahl = 0;
foreach (Abgeordneter abgeordneter in abgeordnete)
{
if (abgeordneter.gibPartei() == partei)
{
anzahl++;
}
}
return anzahl;
}
}
Special types of methods
Constructor methods
Constructors are special methods with no result type and whose name is the same as the name of the class . So you have to start looking at methods in the context in which they are used or are defined. As the name suggests, constructors are used to “create” (construct) objects based on a certain model. Furthermore, one can differentiate between implicit (predefined) and explicit (self-created) constructors. The task of constructors is to initialize the variables of the object, i. H. assign a value to them.
In some languages, such as C ++, there are also destructors as counterparts to the constructors . In Java there are finalization methods instead .
main
Method in Java
In the Java programming language, methods can only be executed if they are called from an object . However, these objects must be created beforehand. When a program is started, there are only classes and not yet any objects. The beginning has to be set somewhere and that happens in Java with the method. Classes that contain a method are executable classes because the method is called directly from the Java virtual machine . For example, the call ensures that the runtime environment tries to find and execute a method in the class with a name , return type and a string array as parameters .
main
main
main
java Beispielklasse
Beispielklasse
main
void
public class Beispielklasse {
public static void main(String[] args) {
for (String arg: args) {
System.out.println("Parameter: " + arg);
}
}
}
Method overloading
When overloading methods, two or more methods are given the same name. Which of these methods is actually called depends on the parameters : the method is executed whose formal parameters correspond to the actual parameters with which the method is called.
Access methods
The access methods are actually normal methods that can query or change individual properties (attributes) of an object . It has become common in the programming style of object-oriented programming to provide appropriate methods with the prefixes get
or set
. In the Java programming language in particular , these methods are also called getter and setter . In contrast, in Microsoft Visual FoxPro, the keywords for this concept are Access and Assign methods.
Multi-methods
In some languages there are so-called multi - methods which operate simultaneously on several parameter objects and can therefore be polymorphic in several parameters .
Virtual methods
A virtual method is a method in which it is only determined at runtime which specific implementation is being used. This so-called dynamic binding enables methods to be overwritten or overloaded . The concept of virtual methods is implemented by a compiler using, for example, virtual tables .
Static methods
Some object-oriented programming languages, such as Java, differentiate between methods that are executed by an object ( instance methods or object methods ) and those that are executed without an object ( static methods or class methods ). Instance methods can access the state of the concrete object, while static methods only work with properties of the class . The main method mentioned above is an example of a static method.
literature
- Laura Lemay, Charles L. Perkins: Java in 21 days . 2000, ISBN 3-8272-5578-3 .
- Katharina Morik, Volker Klingspor: compact information technology. A basic introduction to Java . Springer, 2006, ISBN 3-540-24304-6 .
Individual evidence
- ↑ Access and Assign Methods. In: Visual Studio .NET 2003, Visual FoxPro. Microsoft, accessed October 21, 2013 .
- ↑ James Gosling et al. a .: Chapter 8. Classes. In: The Java Language Specification: Java SE 7 Edition. February 28, 2013, accessed January 20, 2014 .