Signature (programming)

from Wikipedia, the free encyclopedia

A signature (or method signature ) defines the formal interface of a function or procedure in programming . It consists of the name of the function as well as the number, type and sequence of the assignment-compatible parameter data types, whereby a distinction can be made between input, output and in / out parameters . In the case of strictly structured programming languages, the type of the return value is also part of the signature, as are the modifiers that define the rules for the visibility and overwriting of a method.

In object-oriented programming, signature inheritance means the transfer of a method defined (and possibly implemented) in the upper class to the subclass. Static methods with differing signatures cannot be overwritten, but can be overloaded in the case of less strictly structured programming languages .

The set of operators of an abstract data type is also called a signature .

Return type

In Pascal and C descendants and similar languages, which do not take the return type into account when overloaded , it is not possible to distinguish which method should be used in a specific application.

Example:

    ...
     // Deklaration
    float  teile( int dividend, int divisor);    // die Signatur ist hier: teile( int, int )
    double teile (int a, int b);  // nicht erlaubt, Signatur ist dieselbe wie oben: teile( int, int )
    ...

Both methods or functions above would have the same signature.

     ...
     // Deklaration
     boolean isNegativ( float zahl );   // die Signatur ist isNegativ(float)
     boolean isNegativ( double zahl );  // in Ordnung, Signatur ist anders: isNegativ(double)
     ...

Now it could no longer be decided which function divide (int, int) should be used in a specific application:

      ...
    // Die obigen Funktionen sollen hier benutzt werden:
    boolean b = isNegativ( teile(x,y) );   // float teile(..) oder double teile(..) ?
     ...

In contrast to this, it is possible to explicitly specify a type in expressions. This allows the call target to be clearly specified. Haskell makes use of this. For example, the expressions give

    read "42" :: Integer

and

    read "42" :: Double

different values ​​of different types.

Object orientation

In object-oriented programming , a signature is the formal interface of a method .

Signatures play a role in polymorphism , one of the basic concepts of object orientation . In many programming languages, a method of a derived class can override the method of a base class if and only if the signatures of the two methods are identical.

Overridden Methods

The following Java -Example the method signatures of top class and derived class seem to be the same: String redeMit( String ). For the compiler, however, the methods have different signatures because it still takes the associated namespaces and class types into account. The method of the derived class has overwritten the method of the base class here (English  override ). Here's an example:

// Superklasse
class Person {
    String redeMit( String name ) {
        return "Hallo " + name;
    }
}

// Abgeleitete Klasse. Superklassenrelation impliziert Supertyprelation.
class NettePerson extends Person {
    // Methode redeMit(String) wird überschrieben
    String redeMit( String name ) {
        return "Schön Dich zu sehen, " + name;
    }
}

public class Main {
    public static void main( String[] args ) {
        Person p;

        // Hier kann erst zur Laufzeit entschieden werden, welche Methode genommen werden soll.
        if ( 0.5 < Math.random() ) {
            p = new Person();        // originale Person p
        } else {
            p = new NettePerson();   // Person p wird mit "nettem" Verhalten instanziert.
        }

        for ( String name: args ) {
            System.out.println( p.redeMit( name ) + "." );
        }
    }
}

At runtime, each object carries a pointer to a table of virtual methods that exists for each class. This pointer is initialized when the object is instantiated and thus enables polymorphism , i.e. overwritable behavior.

Subclass instances can, if necessary, still access the overwritten methods of their superclasses by means of so-called super pointers.

Class signature

The set of all public signatures defines the interface of a class.

Internal representation in programming languages

Many C ++ - compiler form from the name of a function or method and the encoded signature called a decorated function names ( English mangled name ). This composite name forms the linker symbol . This can prevent functions with the same name but different signature from being incorrectly linked to one another by the linker. The names of methods also contain the class name. However, the decorated function names can only be interpreted for the appropriate compiler or linker, as the following example shows:? Seekpos @? $ Basic_streambuf @ DU? $ Char_traits @ D @ std @@@ std @@ MAE? AV? $ Fpos @ H @ 2 @ V32 @ H @ Z? Seekpos @? $ Basic_streambuf @ GU? $ Char_traits @ G @ std @@@ std @@ MAE? AV? $ Fpos @ H @ 2 @ V32 @ H @ Z

The Java programming language also has an internal representation of method names, the so-called method descriptor . In contrast to C ++, this part of the language specification is identical for all compilers and virtual machines . The following example shows the internal form of the signature of the method " Object meineMethode(int i, double d, Thread t)".

(IDLjava/lang/Thread;)Ljava/lang/Object;

See also

Individual evidence

  1. Björn Kellermann: 1.3 signature, definition by Haskell, accessed on February 3, 2014
  2. Thomas Briner-Lienhard: Keywords for program structure. (PDF) Oberon, accessed on February 3, 2014
  3. ^ Signature (functions) , Mozilla.org, accessed March 25, 2018
  4. Static Methods , Computer Science, Princeton University, accessed March 25, 2018
  5. R. Dumke: Introduction, Algorithms and Data Structures. ( Memento of the original from April 30, 2006 in the Internet Archive ) Info: The @1@ 2Template: Webachiv / IABot / ivs.cs.uni-magdeburg.de archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. Otto-von-Guericke University Magdeburg
  6. ^ Rebecca Wirfs-Brock, Brian Wilkerson, Lauren Wiener: Object-oriented software design. Carl Hanser Verlag, Munich 1993, ISBN 3-446-16319-0
  7. ^ Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification: Second Edition. Section 4.3.3 Method Descriptors. ( Memento of the original from July 8, 2013 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / docs.oracle.com