En programmation orientée objet, une classe déclare des propriétés communes à un ensemble d'objets. La classe déclare des attributs représentant l'état des objets et des méthodes représentant leur comportement.
Une classe représente donc une catégorie d'objets. Il apparaît aussi comme un moule ou une usine à partir de laquelle il est possible de créer des objets. (C'est en quelque sorte une « boîte à outils » qui permet de fabriquer un objet). On parle alors d'un objet en tant qu'instance d'une classe (création d'un objet ayant les propriétés de la classe).
Il est possible de restreindre l'ensemble d'objets représenté par une classe A grâce à un mécanisme d'héritage. Dans ce cas, on crée une nouvelle classe B liée à la classe A et qui ajoute de nouvelles propriétés. Dans ce cas, différents termes sont utilisés :
Dans les exemples ci-dessous est définie dans différents langages une classe Point avec deux attributs x et y. Cette classe contient un constructeur, deux méthodes retournant la valeur des attributs (getX() et getY()), une méthode déterminant si le point représente l'origine (isOrigin()) et une méthode effectuant une translation.
class Point { int x; int y; public: Point(int x, int y) : x(x), y(y) {} int getX() const { return x; } int getY() const { return y; } bool isOrigin() const { return x == 0 && y == 0; } Point translate(const Point& point) const { return Point(x + point.x, y + point.y); } };
public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public boolean isOrigin() { return (x == 0) && (y == 0); } public Point translate(Point point) { return new Point(x + point.x, y + point.y); } }
class Point { private $x; private $y; public function __construct($x, $y) { $this->x = $x; $this->y = $y; } public function getX() { return $this->x; } public function getY() { return $this->y; } public function isOrigin() { return ($this->x == 0) && ($this->y == 0); } public function translate($point) { return new Point($this->x + $point->x, $this->y + $point->y); } }
class Point attr_reader :x, :y def initialize(x, y) @x = x.to_i @y = y.to_i end def origin? @x.zero? and @y.zero? end def translate p Point.new(@x + p.x, @y + p.y) end end
type TPoint = class //définition du type class protected X, Y: integer; public constructor Point(X, Y: integer); function GetX: integer; function GetY: integer; function IsOrigin: Boolean; function Translate(Pt: TPoint): TPoint; end; {La suite contient l'implémentation de la classe: en Pascal il est nécessaire de faire une distinction claire entre code et déclaration.} constructor TPoint.Point(X, Y: integer); begin Self.X := X; //ambigüité possible donc ajout de "Self." Self.Y := Y; end; function TPoint.GetX: integer; begin Result := X; end; function TPoint.GetY: integer; begin Result := Y; end; function TPoint.IsOrigin: Boolean; begin Result := (X = 0) and (Y = 0); end; function TPoint.Translate(Pt: TPoint): TPoint; begin Result := TPoint.Point(X + Pt.X, Y + Pt.Y); end;
class Point: def __init__(self, x, y): self.x = x self.y = y def getX(self): return self.x def getY(self): return self.y def isOrigin(self): return (self.x == 0) and (self.y == 0) def translate(self, point): return Point(self.x + point.x, self.y + point.y)
Public class Point protected x, y As Integer Public Sub New(ByVal x As Integer, ByVal y As Integer) me.x = x me.y = y End Sub Public Function getX As Integer return x End Function Public Function getY As Integer return y End Function Public Function isOrigine As Boolean If( x = 0 And y = 0)then return True Else return False End If End Function Public Function translate(ByVal p As Point) As Point return New Point(x + p.x, y + p.y) End Function
package Point_Pack is -- une classe en Ada est un type dit "étiqueté" -- ici on décide en plus de cacher le contenu du type (partie privée) type Point is tagged private; -- les méthodes sont déclarées en dehors du type function Get_X (P: Point) return Integer; function Get_Y (P: Point) return Integer; function Is_Origin (P: Point) return Boolean; function Translate (P, Vector: Point) return Point; -- pas de constructeur "intégré" function New_Point (X, Y: Integer) return Point; private -- définitions inaccessibles à l'utilisateur du type type Point is tagged record X, Y: Integer; end record; end Point_Pack; -- la suite contient l'implémentation de la classe: en Ada il est important de séparer -- le code (le corps) et les déclarations "visibles" (la spécification): en général dans 2 fichiers différents package body Point_Pack is -- les méthodes nomment explicitement l'objet en argument function Get_X (P: Point) return Integer is begin return P.X; end Get_X; function Get_Y (P: Point) return Integer is begin return P.Y; end Get_Y; function Is_Origin (P: Point) return Boolean is begin return P.X = 0 and then P.Y = 0; end Is_Origin; function Translate (P, Vector: Point) return Point is begin return Point'( X => P.X + Vector.X, Y => P.Y + Vector.Y ); end Translate; function New_Point (X, Y: Integer) return Point is begin return Point'(X => X, Y => Y); end New_Point; end Point_Pack;