Comparison of programming languages (object-oriented programming)
From Wikipedia, the free encyclopedia
Contents |
[edit] Object creation and destruction
Statements in square brackets are optional.
creation | destruction | |
---|---|---|
C++ (STL) | class variable(parameters); or class *variable = new class(parameters); |
delete pointer; |
C# | class variable = new class(parameters); | variable.Dispose();[1] |
Java | variable.dispose();[1] | |
JavaScript | var variable = new class (parameters) | delete (variable);[1] |
Python | variable = class(parameters) | del variable [1](Normally not needed) |
Visual Basic .NET | Dim variable As New class(parameters) | variable.Dispose()[1] |
PHP | $variable = new class(parameters); | unset($variable); |
Perl | $variable = class->new(parameters); | undef($variable); |
Windows PowerShell | $variable = New-Object «-TypeName» class ««-ArgumentList» parameters» | Remove-Variable «-Name» variable |
OCaml | let variable = new class parameters or let variable = object «(self)» «inherit ...» members end |
[1] |
[edit] File declaration
class | interface | namespace | |
---|---|---|---|
C++ (STL) | class name« : public parentclasses» { members }; | namespace name { members } | |
C# | class name« : parentclass«, interfaces»» { members } | interface name { members } | |
Java | class name« extends parentclass»« implements interfaces» { members } | package name; members | |
JavaScript | [2] | [2] | |
Python | class name(parentclasses): Tab members |
same, declare methods with pass as body | __all__ = [ member1,member2,... ] |
Visual Basic .NET | Class name« Inherits parentclass»« Implements interfaces» members End Class |
Interface name members End Interface |
Namespace name members End Namespace |
PHP | class name« extends parentclass»« implements interfaces» { members } | interface name { members } | namespace name; members |
Perl | package name; «@ISA = qw(parentclasses);» members 1; | package name; members | |
Windows PowerShell | [2] | ||
OCaml | class name «parameters» = object «(self)» «inherit parentclass «parameters» «inherit parentclass «parameters» ...»» members end |
[edit] Class members
[edit] Constructors and destructors
constructor | destructor/finalizer | |
---|---|---|
C++ (STL) | class(parameters) { instructions } | ~class() { instructions } |
C# | ~class() { instructions }[3] | |
Java | void finalize() { instructions }[3] | |
JavaScript | function class(parameters) { instructions } | [2] |
Python | def __init__( self, parameters): Tab instructions |
def __del__(self): Tab instructions |
Visual Basic .NET | Sub New(parameters) instructions End Sub |
|
PHP | function __construct(parameters) { instructions } | function __destruct() { instructions } |
Perl | sub new { my ($class, parameters) = @_; my $self = {}; instructions bless($self, $class); return $self; } | sub DESTROY { my ($self) = @_; instructions } |
Windows PowerShell | [2] | [2] |
OCaml | initializer instructions | [2] |
[edit] Fields
How to declare a field field
public | private | protected | friend | |
---|---|---|---|---|
C++ (STL) | public: type field; | private: type field; | protected: type field; | |
C# | public type field; | private type field; | protected type field; | friend type field; |
Java | type field; | |||
JavaScript | this.field« = value»; | var field« = value»; | [2] | [2] |
Python | 'self.field = expression Just assign a value to it in a method |
[2] | ||
Visual Basic .NET | Public field As type | Private field As type | Protected field As type | Friend field As type |
PHP | public $field; | private $field; | protected $field; | |
Perl | $self->{field} = expression; Just assign a value to it in a method |
[2] | ||
Windows PowerShell | Add-Member «-MemberType »NoteProperty «-Name »Bar «-Value »value -InputObject variable |
[2] | ||
OCaml | [2] | [2] | val «mutable» field = value | [2] |
[edit] Methods
basic | value-returning method | overloaded operator | ||
---|---|---|---|---|
unary | binary | |||
C++ (STL) | void foo(«parameters») { instructions } | type foo(«parameters») { instructions } | type operator symbol() { instructions } | type operator symbol(type operand2) { instructions } |
C# | static type operator symbol(type operand1) { instructions } | static type operator symbol(type operand1, type operand2) { instructions } | ||
Java | [2] | |||
JavaScript | function foo(«parameters») { instructions }; or var foo = function («parameters») { instructions }; or var foo = new Function («"parameters1", "parameters2", ..., »"instructions"); |
|||
Python | def foo(«self , »«parameters»): Tab instructions |
def foo(«self , »«parameters»): Tab instructions Tab return value |
def __neg__(self): Tab instructions Tab return value |
def __mul__(self, operand2): Tab instructions Tab return value |
Visual Basic .NET | Sub Foo(«parameters») instructions End Sub |
Function Foo(«parameters») As type instructions End Function |
Shared Operator symbol(operand1 As type) As type instructions End Operator |
Shared Operator symbol(operand1 As type, operand2 As type) As type instructions End Operator |
PHP | function foo(«parameters») {instructions } | [2] | ||
Perl | sub foo { my ($self, parameters) = @_; instructions } | use overload "symbol" => "method"; | ||
Windows PowerShell | Add-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions } -InputObject variable | [2] | ||
OCaml | method foo «parameters» = expression | [2] |
[edit] Properties
How to declare a property named "Bar"
read-write | read-only | write-only | |
---|---|---|---|
C++ (STL) | [2] | ||
C# | type Bar { get { instructions } set { instructions } } |
type Bar { get { instructions } } | type Bar { set { instructions } } |
Java | [2] | ||
JavaScript | [2] | ||
Python | def setBar( self, parameter): instructions def getBar(self): instructions bar = property( getBar,setBar ) |
def getBar(self): instructions bar = property( getBar ) |
def setBar(self, parameter): instructions bar = property( fset = setBar ) |
Visual Basic .NET | Property Bar As type Get instructions End Get Set (ByVal Value As type) instructions End Set End Property |
ReadOnly Property Bar As type Get instructions End Get End Property |
WriteOnly Property Bar As type Set (ByVal Value As type) instructions End Set End Property |
PHP | function __get($property) { switch ($property) { case 'Bar' : instructions } } function __set($property, $value) { switch ($property) { case 'Bar' : instructions } } |
function __get($property) { switch ($property) { case 'Bar' : instructions } } |
function __set($property, $value) { switch ($property) { case 'Bar' : instructions } } |
Windows PowerShell | [2] | Add-Member «-MemberType »ScriptProperty «-Name »Bar «-Value »{ instructions } -InputObject variable |
[2] |
OCaml | [2] |
[edit] Member access
How to access members of an object x
method | property/field | |
---|---|---|
C++ (STL) | x.method(parameters) or ptr->method(parameters) |
x.property or ptr->property |
C# | x.method(parameters) | x.property |
Python | ||
Java | ||
Visual Basic .NET | ||
Windows PowerShell | ||
PHP | x->method(parameters) | x->property |
Perl | x->{property} | |
JavaScript | x.method(parameters) or x["method"](parameters) |
x.property or x["property"] |
OCaml | x#method «parameters» | [2] |
[edit] Special variables
current object | current object's parent class | |
---|---|---|
C++ (STL) | *this | |
C# | this | base |
Java | super | |
JavaScript | ||
Python | self | |
Visual Basic .NET | Me | MyBase |
Windows PowerShell | ||
PHP | $this | parent |
Perl | ||
OCaml |
[edit] See also
[edit] Notes
- ^ a b c d e f This language uses garbage collection to release unused memory.
- ^ a b c d e f g h i j k l m n o p q r s t u v w x y z This language does not support this feature
- ^ a b This is a finalizer rather than a destructor. It is called by the garbage collector when an object is about to be garbage-collected. There is no guarantee on when it will be called or if it will be called at all.