Class (computer programming)

Taking a class (also called object type ) is understood in the object- oriented programming, an abstract model and a blueprint for a series of similar objects.

The class serves as a blueprint for the imaging of real objects in software objects and describes attributes ( properties ) and methods ( behaviors ) of the objects. Generalizing, one could also say that a class corresponds to the data type of an object. Formally thus has a class for program execution time is no memory, but only the objects that were instantiated from it.

Heredity

Classes can communicate with each other in hierarchical relationships and more complex structures. The laws by which they are formed, describes the basic concept of inheritance. Here are also the terms base class and derived class of importance to characterize the relationships between classes. The base class describes common properties, so it is a generalization of the derived classes; these are thus specializations of the base class.

Example: base class motor vehicle is a generalization of the derived classes ( specializations ) car, truck, motorcycle and tractor.

The derived classes inherit all the properties and methods of the base class (that is, a motorcycle has all the characteristics of a motor vehicle, and you can do anything with it that you can do with a motor vehicle ). In addition, the derived class introduces additional properties and methods that are possible with their objects. ( The bike has, for example, a luggage rack, a car is not, but a trunk. )

Programming style

In many programming languages ​​, it is common that a class name begins with an uppercase letter, variable names, however, with a lowercase letter.

Composite

Similarly, the class of the composite is a tool for managing of related attributes. He is a composite data type from several other data types. The individual components can be regarded as attributes of the new composite type - in it but no methods are described.

Example

For example, consider a lamp. A lamp may have different properties ( attributes), such as color, weight and whether they are lit. Since you can operate little with the properties color and size, a useful behavior would thus a light switch method, which determines the current state of on and off. An abstract notation might look like this:

Class lamp {        / / Properties        gehaeusefarbe; / / Type: { NUMBER, NUMBER, NUMBER } { red, green, blue } for        weight; / / NUMBER        light color; / / Type: { NUMBER, NUMBER, NUMBER } { red, green, blue } for        brightness; / / NUMBER          / / Methods        Switch ();        off ();    } Concepts such as inheritance could describe the fact that there are different types of lamps, such as street lights, flashlights or car headlights. These special lamps would then be subclasses of the class lamp, that is, they would extended attributes (eg Taschenlampe.maximaleLeuchtdauer, Autoscheinwerfer.Kippbarkeit ) and methods ( eg Taschenlampe.batterieLaden () Autoscheinwerfer.fernlichtEinschalten ()) possess. For special classes the lamp class is a superclass.

Program Example

Coding example in Python

The following example is written in the Python programming language:

Vehicle class (object):      # The "Vehicle" class is the base class.        def move ( self):          print " vehicle is moved "      move # end # end class vehicle   class car ( vehicle):      # The class " car " is the derived class.        def move ( self):          print " car moves "      move # end # end class Car   def run (vehicle ):      # to illustrate the so-called " polymorphism "      fahrzeug.bewegen (); # drive end   # - Main program - vehicle = vehicle () auto = car ( )   fahrzeug.bewegen () auto.bewegen ()   # Polymorphism: Method ' go ' driving (vehicle ) drive (auto) # - end main program - explanation

This program defines a class vehicle and a derived class car.

The base class has a method called move (), the text " vehicle is moved. " On the computer monitor outputs. The derived class of vehicle car also has a move () method and overrides the method of the vehicle. The output generated by it is " car is moved. ".

Then the definition of an independent function go (), which gets an object of the base class as an argument follows. The method move () is called on this object.

Finally, the main program, which both an object of the base class (vehicle ) follows, as well as the derived class (auto) generates, and move on both first ( ) calls and then continue with the help of ( ) also once again move () for both objects performs.

Running this program will appear on the screen:

Vehicle is moved.    Car is moved.    Vehicle is moved.    Car is moved. It can be seen, is that although the function go ( ) for a vehicle defined, it also works for a car and the overridden method is called. That is, it works for objects of the base class, and for all objects of derived classes (which inherit the properties and yes "may" therefore everything the base class 'may' ). This (generally desired ) behavior is called polymorphism.

Extension

An extension or abstraction of this concept is found in the model of abstract classes and metaclasses.

It is also a so-called anonymous class. In this case, a class is described only at the exact location at which an object is generated from it; it is not separated (eg in a separate file ) as a separate component in the source code, and therefore can not be reused by other parts of the program or specifically addressed. The class has also not have its own name. Usually, however, it inherits from another, then describes the main characteristics and methods of the object for its future use. The derived, unnamed class modifies the behavior usually only slightly.

Example ( Java):

/ / Declare a variable to contain an object of class "Button"    java.awt.Button help button;      / / Create a button object store in help button    help button = new java.awt.Button ( "Help "); / / "Help" is the label of the button    / / Assign to the button an object that has a method " actionPerformed "    / / ( Is called when the button is clicked ).    hilfeButton.addActionListener (new java.awt.event.ActionListener () {      void actionPerformed ( ActionEvent e)      {        System.out.println ( "Help text ");      }    }); / / End anonymous class It is produced with new an object in a main java.awt.event.ActionListener corresponds (though no base class, but an interface ). The actionPerformed method is overridden so that it outputs the help text on the screen as a special practice exactly this object. As a specialized behavior has been defined, the object of a derived class, not by action listener directly - but no class name was specified. In the following program, the object can only be used as an action listener ( polymorphism ).

Some programming languages ​​allow the change of classes at runtime, and the subsequent modification of the structure of an object (add / remove properties or methods). This so-called " reflection " should be used only in an emergency, as the program making it very difficult to understand.

It is sometimes (similar to an anonymous class ) also defines an inner classes. Difference to a "normal " classes is initially the visibility region - an inner class defined within another ( " outer class "). If it is private, so only objects of the outer class can create and use objects of the inner. If the inner class non- static, so one object creation is even dependent on an object of the outer class, and only one such object possible.

68244
de