Class (computer science)
In computing, a class is a template for the purpose of creating data objects according to a predefined pattern. Classes are used to represent entities or concepts, such as nouns in language. Each class is a model that defines a set of variables and appropriate methods to operate on said data. Each object created from the class is called an instance of the class.
Object-oriented programming is the main foundation for object types. They allow abstracting the data and its associated operations in the manner of a black box. Programming languages that support classes differ slightly in their support for various class-related features. Most support various forms of inheritance. Many languages also support features to provide encapsulation, such as access specifiers.
A class may also have a run-time representation (meta-object), which provides run-time support for manipulation of class-related metadata.
Components
Classes are made up of elements, generically called “members”, of various types:
- data fields: they store the state of the class through variables, data structures and even other classes.
- methods: subroutines of handling such data.
- certain languages allow a third type of member: the "properties", halfway between fields and methods.
Using a language simile, if classes represent nouns, data fields can be nouns or adjectives, and methods are verbs.
The typical syntax of a class is:
class Name { // Variables member (usually private) member_1; //list of members member_2; member_3; // Functions or methods (usually public) function_member_1( ); // known member functions function_member_2 ( ); // functions as methods // Properties (usually public) property_1; property_2; property_3; property_4; !
Data fields
Members or variables are used to hold data that reflects the state of the class. Data can be stored in variables, or more complex structures, such as structs, unions, and even other classes.
Usually, member variables are private to the object (following the design guidelines of the Hiding Principle) and are accessed via properties or methods that perform additional checks.
Methods in classes
Methods implement the functionality associated with the object. Methods are the equivalent of functions in structured programming. They differ from them in that it is possible to access the variables of the class implicitly or included.
When you want to perform an action on an object, you say that you send it a message calling a method that will perform the action.
Properties
Properties are the attributes of the computer. Since it is common for member variables to be private to control access and maintain consistency, the need arises to allow querying or modifying their value through pairs of methods.
Modern object-oriented languages add the property construct, which is a simplified syntax for such methods.
Contenido relacionado
AS/400
Luzer
CSS