Teknik Informatika    
   
Daftar Isi
(Sebelumnya) Comparison of programming lang ...Comparison of programming lang ... (Berikutnya)

Perbandingan -- programming languages (object-oriented programming)

This Perbandingan -- programming languages compares how object-oriented programming languages such as C++, Python, Perl, Java, Object Pascal and others manipulate data structures.

Contents


Object construction and destruction

constructiondestruction
ABAP Objectsdata variable type ref to class .
create object variable «exporting parameter = argument[1]».
[2][3]
C++ (STL)class variable«(parameters)»; or
class *variable = new class«(parameters)»;
delete pointer;
C#class variable = new class(parameters);variable.Dispose();[3]
Javavariable.dispose();[3]
Ddelete variable;
Objective-C (Cocoa)class *variable = [[class alloc ] init]; or
class *variable = [[class alloc ] initWithFoo:parameter «bar:parameter ...»];
[variable release];
Pythonvariable = class(parameters)del variable[3] (Normally not needed)
Visual Basic .NETDim variable As New class(parameters)variable.Dispose()[3]
Eiffelcreate variable or
create «{TYPE}» variable.make_foo «(parameters)» or
variable := create {TYPE} or
variable := create {TYPE}.make_foo «(parameters)»
[3]
PHP$variable = new class(parameters);unset($variable);
Perl 5«my »$variable = class->new«(parameters)»;undef($variable);
Perl 6«my »$variable = class.new«(parameters)»;$variable.undefine;
Rubyvariable = class.new«(parameters)»[3]
Windows PowerShell$variable = New-Object «-TypeName» class ««-ArgumentList» parameters»Remove-Variable «-Name» variable
OCamllet variable = new class «parameters» or
let variable = object members end[4]
[3]
F#let variable = «new »class(«parameters»)
Smalltalk"The class is an Object.
Just send a message to a class, usually #new or #new:, and many others, for example:"
Pointy x: 10 y: 20.
Array with: -1 with: 3 with: 2.
 
JavaScriptvar variable = new class«(parameters)» or
var variable = { «key1: value1«, key2: value2 ...»»}
[3]
Object Pascal / DelphiClassVar := ClassType.ConstructorName(parameters);ClassVar.Free;

Class declaration

classprotocolnamespace
ABAP Objectsclass name definition «inheriting from parentclass». «interfaces: interfaces.» method_and_field_declarations endclass.
class name implementation. method_implementations endclass.
interface name. members endinterface.N/A
C++ (STL)class name« : public parentclasses[5]» { members }; namespace name { members }
C#class name« : «parentclass»«, interfaces»» { members }interface name« : parentinterfaces» { members }
Dmodule name;
members
Javaclass name« extends parentclass»« implements interfaces» { members }interface name« extends parentinterfaces» { members }package name; members
PHPnamespace name; members
Objective-C@interface name« : parentclass[6]»«< protocols >» { instance_fields } method_and_property_declarations @end
@implementation
name method_implementations @end[7]
@protocol name«< parentprotocols >» members @endPrefixes to class and protocol names conventionally used as a kind of namespace
Pythonclass name«(parentclasses[5])»:
Tab
members
[8]__all__ = [ member1,member2,... ]
Visual Basic .NETClass name« Inherits parentclass»« Implements interfaces»
members
End Class
Interface name« Inherits parentinterfaces»
members
End Interface
Namespace name
members
End Namespace
Eiffelclass name« inherit parentclasses[5]»
members
end
N/A
Perlpackage name; «@ISA = qw(parentclasses[5]);» members 1; package name; members
Perl 6class name «is parentclass «is parentclass ...[5]»» «does role «does role ...»» { members }role name «does role «does role ...»» { members }module name { members }
Rubyclass name« < parentclass»
members
end
 module name
members
end
Windows PowerShellN/A
OCamlclass name «parameters» = object «(self)» «inherit parentclass «parameters» «inherit parentclass «parameters» ...[5]»» members end module name
members
F#type name«(parameters)» «as this» = class «inherit parentclass«(parameters)» «as base»» members «interface interface with implementation «interface interface with implementation ...»» endtype name = interface members endnamespace name
members
Smalltalk"The class is an Object.
Just send a message to the superclass (st-80) or the destination namespace (Visualworks)"
 "The namespace is an Object.
Just send a message to the parent namespace"

Class members

Constructors and destructors

constructordestructorfinalizer[9]
ABAP Objectsmethods constructor «importing parameter = argument»
method constructor. instructions endmethod.[10]
N/A 
C++ (STL)class(«parameters») «: initializers[11]» { instructions }~class() { instructions } 
C#class(«parameters») { instructions }void Dispose(){ instructions }~class() { instructions }
Dthis(«parameters») { instructions } ~this() { instructions }
Javaclass(«parameters») { instructions }void dispose() { instructions }void finalize() { instructions }
Eiffel[12] [13]
Objective-C (Cocoa)- (id)init { instructions... return self; } or
- (id)initWithFoo:parameter «bar:parameter ...» { instructions... return self; }
- (void)dealloc { instructions }- (void)finalize { instructions }
Pythondef __init__(self«, parameters»):
Tab instructions
 def __del__(self):
Tab instructions
Visual Basic .NETSub New(«parameters»)
instructions
End Sub
Sub Dispose()
instructions
End Sub
Overrides Sub Finalize()
instructions
End Sub
PHPfunction __construct(«parameters») { instructions }function __destruct() { instructions } 
Perlsub new { my ($class«, parameters») = @_; my $self = {}; instructions ... bless($self, $class); return $self; }sub DESTROY { my ($self) = @_; instructions } 
Perl 6submethod BUILD { instructions } or
«multi » method new(««$self: »parameters») { self.bless(*, field1 => value1, ...); ... instructions }
submethod DESTROY { instructions } 
Rubydef initialize«(parameters)»
instructions
end
N/A 
Windows PowerShellN/A
OCamlinitializer instructions[14]N/A 
F#do instructions or
new(parameters) = expression[15]
member this.Dispose() = instructionsoverride this.Finalize() = instructions
JavaScriptfunction name«(«parameters») { instructions }
(In JavaScript constructor itself is an object.)
N/A

Fields

publicprivateprotectedfriend
ABAP Objectspublic section.[16] data field type type.private section.[16] data field type type.protected section.[16] data field type type.[17]
C++ (STL)public: type field;private: type field;protected: type field;[18]
C#public type field «= value»;private type field «= value»;protected type field «= value»;internal type field «= value»;
D package type field «= value»;
Javaprotected type field «= value»;type field «= value»;
Eiffelfeature
field: TYPE
feature {NONE}
field: TYPE
feature {current_class}
field: TYPE
feature {FRIEND}
field: TYPE
Objective-C@public type field;@private type field;@protected type field;@package type field;
SmalltalkN/A"Just send a message to the class"
class addInstVarName: field.
class removeInstVarName: field.
N/A
Pythonself.field = value
Just assign a value to it in a method
self.__field = value
Just assign a value to it in a method
N/A
Visual Basic .NETPublic field As type «= value»Private field As type «= value»Protected field As type «= value»Friend field As type «= value»
PHPpublic $field «= value»;private $field «= value»;protected $field «= value»; 
Perl$self->{field} = value;
Just assign a value to it in a method
N/A
Perl 6has« type »$.field« is rw»has« type »$!fieldN/A
RubyN/A@field = value
Just assign a value to it in a method
 
Windows PowerShellAdd-Member
«-MemberType »NoteProperty
«-Name »Bar «-Value »value
-InputObject variable
N/A
OCamlN/Aval «mutable» field = valueN/A
F#N/Alet «mutable» field = valueN/A
JavaScriptthis.field = value
this["field"] = value
Just assign a value to it in a method
   

Methods

basic/void methodvalue-returning method
ABAP Objectsmethods name «importing parameter = argument» «exporting parameter = argument» «changing parameter = argument» «returning value(parameter)»
method name. instructions endmethod.[19]
[20]
C++ (STL)[21]void foo(«parameters») { instructions }type foo(«parameters») { instructions ... return value; }
C#
D
Java
Eiffelfoo ( «parameters» )
do
instructions
end
foo ( «parameters» ): TYPE
do
instructions...
Result := value
end
Objective-C- (void)foo«:parameter «bar:parameter ...»» { instructions }- (type)foo«:parameter «bar:parameter ...»» { instructions... return value; }
Pythondef foo(self«, parameters»):
Tab
instructions
def foo(self«, parameters»):
Tab
instructions
Tab return
value
Visual Basic .NETSub Foo(«parameters»)
instructions
End Sub
Function Foo(«parameters») As type
instructions
...
Return value
End Function
PHPfunction foo(«parameters») { instructions }function foo(«parameters») { instructions ... return value; }
Perlsub foo { my ($self«, parameters») = @_; instructions }sub foo { my ($self«, parameters») = @_; instructions ... return value; }
Perl 6«has »«multi »method foo(««$self:  »parameters») { instructions }«has «type »»«multi »method foo(««$self:  »parameters») { instructions ... return value; }
Rubydef foo«(parameters)»
instructions
end
def foo«(parameters)»
instructions
expression resulting in return value
end
or
def foo«(parameters
instructions
return value
end
Windows PowerShellAdd-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions } -InputObject variableAdd-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions ... return value } -InputObject variable
OCamlN/Amethod foo «parameters» = expression
F#member this.foo(«parameters») = expression
JavaScriptthis.method = function(«parameters») {instructions}
name«.prototype.method = function(«parameters») {instructions}
Just assign a function to it in a method
this.method = function(«parameters») {instructions... return value;}
name«.prototype.method = function(«parameters») {instructions... return value;}
Just assign a function to it in a method

Properties

How to declare a property named "Bar"

Manually implemented

read-writeread-onlywrite-only
ABAP ObjectsN/A
C++ (STL)N/A
C#type Bar {
get {
instructions ... return value; }
set {
instructions } }
type Bar { get { instructions ... return value; } }type Bar { set { instructions } }
D@property type bar() { instructions ... return value; }
@property
type bar(type value) { instructions ... return value; }
@property type bar() { instructions ... return value; }@property type bar(type value) { instructions ... return value; }
JavaN/A
Objective-C 2.0 (Cocoa)@property (readwrite) type bar;
and then inside @implementation
- (type)bar { instructions }
- (void)setBar:(type)value { instructions }
@property (readonly) type bar;
and then inside @implementation

- (type)bar { instructions }

 
Eiffelfeature -- Access
x: TYPE assign set_x
feature -- Settings
set_x (a_x: like x) do instructions ensure x_set: verification end
  
Pythondef setBar(self, value):
Tab
instructions
def
getBar(self):
Tab
instructions
Tab return value
bar = property(getBar, setBar)
def getBar(self):
Tab
instructions
Tab return value
bar = property(getBar)
def setBar(self, value):
Tab
instructions
bar = property(fset = setBar)
Visual Basic .NETProperty Bar() As type
Get
instructions
Return value
End Get
Set (ByVal
Value As type)
instructions
End Set
End Property
ReadOnly Property Bar() As type
Get
instructions
Return value
End Get
End Property
WriteOnly Property Bar() As type
Set (ByVal Value As type)
instructions
End Set
End Property
PHPfunction __get($property) {
switch (
$property) {
case '
Bar' : instructions ... return value;
} }
function __set(
$property, $value) {
switch (
$property) {
case '
Bar' : instructions
} }
function __get($property) {
switch ($
property) {
case '
Bar' : instructions ... return value;
} }
function __set($property, $value) {
switch (
$property) {
case '
Bar' : instructions
} }
Perlsub Bar {
my $self = shift;
if (my $Bar = shift) {
# setter
$self->{Bar} = $Bar;
return $self;
} else {
# getter
return $self->{Bar};
}
}
sub Bar {
my $self = shift;
if (my $Bar = shift) {
# read-only
die "Bar is read-only\n";
} else {
# getter
return $self->{Bar};
}
}
sub Bar {
my $self = shift;
if (my $Bar = shift) {
# setter
$self->{Bar} = $Bar;
return $self;
} else {
# write-only
die "Bar is write-only\n";
}
}
Perl 6N/A
Rubydef bar
instructions
expression resulting in return value
end
def bar=(value)
instructions
end
def bar
instructions
expression resulting in return value
end
def bar=(value)
instructions
end
Windows PowerShellAdd-Member
«-MemberType »ScriptProperty
«-Name »Bar «-Value »{ instructions ... return value }
«-SecondValue »{ instructions }
-InputObject variable
Add-Member
«-MemberType »ScriptProperty
«-Name »Bar «-Value »{ instructions ... return value}
-InputObject variable
Add-Member
«-MemberType »ScriptProperty
«-Name »Bar -SecondValue { instructions }
-InputObject variable
OCamlN/A
F#member this.Bar with get() = expression and set(value) = expressionmember this.Bar = expressionmember this.Bar with set(value) = expression

Automatically implemented

read-writeread-onlywrite-only
ABAP ObjectsN/A
C++ (STL)N/A
C#type Bar { get; set; }type Bar { get; private set; }type Bar { private get; set; }
D   
JavaN/A
Objective-C 2.0 (Cocoa)@property (readwrite) type bar;
and then inside @implementation
@synthesize bar;
@property (readonly) type bar;
and then inside @implementation
@synthesize bar;
 
Eiffel   
Python@property
def bar(self):
Tab instructions
@bar.setter
def bar(self, value):
Tab instructions
@property
def bar(self):
Tab instructions
bar = property()
@bar.setter
def bar(self, value):
Tab instructions
Visual Basic .NETProperty Bar As type« = initial_value» (VB 10)  
PHP   
Perl[22]use base qw(Class::Accessor);
__PACKAGE__->mk_accessors('Bar');
use base qw(Class::Accessor);
__PACKAGE__->mk_ro_accessors('Bar');
use base qw(Class::Accessor);
__PACKAGE__->mk_wo_accessors('Bar');
Perl 6N/A
Rubyattr_accessor :barattr_reader :barattr_writer :bar
Windows PowerShell   
OCamlN/A
F#member val Bar = value with get, set  

Overloaded operators

Standard operators

 unarybinaryfunction call
ABAP ObjectsN/A
C++ (STL)type operatorsymbol() { instructions }type operatorsymbol(type operand2) { instructions }type operator()(«parameters») { instructions }
C#static type operator symbol(type operand) { instructions }static type operator symbol(type operand1, type operand2) { instructions }N/A
Dtype opUnary(string s)() if (s == "symbol") { instructions }type opBinary(string s)(type operand2) if (s == "symbol") { instructions }
type opBinaryRight(string s)(type operand1) if (s == "symbol") { switch (s) { instructions }
type opCall(«parameters») { instructions }
JavaN/A
Objective-C
Eiffel[23]op_name alias "symbol": TYPE
do instructions end
op_name alias "symbol" (operand: TYPE1): TYPE2
do instructions end
 
Pythondef __opname__(self):
Tab
instructions
Tab return
value
def __opname__(self, operand2):
Tab
instructions
Tab return
value
def __call__(self«, paramters»):
Tab
instructions
Tab return
value
Visual Basic .NETShared Operator symbol(operand As type) As type
instructions
End Operator
Shared Operator symbol(operand1 As type, operand2 As type) As type
instructions
End Operator
N/A
PHP[24]function __invoke(«parameters») { instructions } (PHP 5.3+)
Perluse overload "symbol" => sub { my ($self) = @_; instructions };use overload "symbol" => sub { my ($self, $operand2, $operands_reversed) = @_; instructions }; 
Perl 6«our «type »»«multi »method prefix:<symbol> («$operand: ») { instructions ... return value; } or
«our «type »»«multi »method postfix:<symbol> («$operand: ») { instructions ... return value; } or
«our «type »»«multi »method circumfix:<symbol1 symbol2> («$operand: ») { instructions ... return value; }
«our «type »»«multi »method infix:<symbol> («$operand1: » type operand2) { instructions ... return value; }«our «type »»«multi »method postcircumfix:<( )> («$self: » «parameters») { instructions }
Rubydef symbol
instructions
expression resulting in return value
end
def symbol(operand2)
instructions
expression resulting in return value
end
N/A
Windows PowerShellN/A
OCaml
F#static member (symbol) operand = expressionstatic member (symbol) (operand1, operand2) = expressionN/A

Indexers

 read-writeread-onlywrite-only
ABAP ObjectsN/A
C++ (STL)type& operator[](type index) { instructions }type operator[](type index) { instructions } 
C#type this[type index] {
get{
instructions }
set{
instructions } }
type this[type index] { get{ instructions } }type this[type index] { set{ instructions } }
Dtype opIndex(type index) { instructions }
type opIndexAssign(type value, type index) { instructions }
type opIndex(type index) { instructions }type opIndexAssign(type value, type index) { instructions }
JavaN/A
Objective-C
Eiffel[23]bracket_name alias "[]" (index: TYPE): TYPE assign set_item
do instructions end
set_item (value: TYPE; index: TYPE):
do instructions end
bracket_name alias "[]" (index: TYPE): TYPE
do instructions end
 
Pythondef __getitem__(self, index):
Tab instructions
Tab return value
def __setitem__(self, index, value):
Tab instructions
def __getitem__(self, index):
Tab instructions
Tab return value
def __setitem__(self, index, value):
Tab instructions
Visual Basic .NETDefault Property Item(Index As type) As type
Get
instructions
End Get
Set(ByVal
Value As type)
instructions
End Set
End Property
Default ReadOnly Property Item(Index As type) As type
Get
instructions
End Get
End Property
Default WriteOnly Property Item(Index As type) As type
Set(ByVal
Value As type)
instructions
End Set
End Property
PHP[25]
Perl[26]
Perl 6«our «type »»«multi »method postcircumfix:<[ ]> is rw («$self: » type $index) { instructions ... return value; } or
«our «type »»«multi »method postcircumfix:<{ }> is rw («$self: » type $key) { instructions ... return value; }
«our «type »»«multi »method postcircumfix:<[ ]>(«$self: » type $index) { instructions ... return value; } or
«our «type »»«multi »method postcircumfix:<{ }> («$self: » type $key) { instructions ... return value; }
N/A
Rubydef [](index)
instructions
expression resulting in return value
end
def []=(index, value)
instructions
end
def [](index)
instructions
expression resulting in return value
end
def []=(index, value)
instructions
end
Windows PowerShellN/A
OCaml
F#member this.Item with get(index) = expression and set index value = expressionmember this.Item with get(index) = expressionmember this.Item with set index value = expression

Type casts

 downcastupcast
ABAP ObjectsN/A
C++ (STL) operator returntype() { instructions }
C#static explicit operator returntype(type operand) { instructions }static implicit operator returntype(type operand) { instructions }
D T opCast(T)() { if (is(T == type)) { instructions } ... }
JavaN/A
Objective-C
Eiffel[23]
Python
Visual Basic .NETShared Narrowing Operator CType(operand As type) As returntype
instructions
End Operator
Shared Widening Operator CType(operand As type) As returntype
instructions
End Operator
Perl 6 multi method type«($self:)» is export { instructions }
PHPN/A
Perl
Ruby
Windows PowerShell
OCaml
F#  

Member access

How to access members of an object x

object memberclass membernamespace member
methodfieldproperty
ABAP Objectsx->methodparameters[27]»).x->fieldN/Ax=>field or x=>methodparameters[27]»).N/A
C++ (STL)x.method(parameters) or
ptr->method(parameters)
x.field or
ptr->field
 cls::memberns::member
Objective-C[x method«:parameter «bar:parameter ...»»]x->fieldx.property (2.0 only) or
[x property]
[cls method«:parameter «bar:parameter ...»»] 
Smalltalkx method«:parameter «bar:parameter ...»»N/A cls method«:parameter «bar:parameter ...»» 
C#x.method(parameters)x.fieldx.propertycls.memberns.member
JavaN/A
Dx.property
Python
Visual Basic .NET
Windows PowerShell[cls]::member
F#N/Acls.member
Eiffelx.method«(parameters)»x.field {cls}.memberN/A
RubyN/Ax.propertycls.member
PHPx->method(parameters)x->fieldx->propertycls::memberns\member
Perlx->method«(parameters)»x->{field} cls->method«(parameters)»ns::member
Perl 6x.method«(parameters)» or
x!method«(parameters)»
x.field or
x!field
 cls.method«(parameters)» or
cls!method«(parameters)»
ns::member
OCamlx#method «parameters»N/A  
JavaScriptx.method(parameters)
x["method"](parameters)
x.field
x["field"]
x.property
x["property"]
N/AN/A

Member availability

Has member?Handler for missing member
MethodFieldMethodField
ABAP ObjectsN/A
C++ (STL)
Objective-C (Cocoa)[x respondsToSelector:@selector(method)]N/AforwardInvocation:N/A
Smalltalkx respondsTo: selectorN/AdoesNotUnderstand:N/A
C#(using reflection)
Java
D  opDispatch()
EiffelN/A
Pythonhasattr(x, "method") and callable(x.method)hasattr(x, "field")__getattr__()
Visual Basic .NET(using reflection)
Windows PowerShell(using reflection)
F#(using reflection)
Rubyx.respond_to?(:method)N/Amethod_missing()N/A
PHPmethod_exists(x, "method")property_exists(x, "field")__call()__get() / __set()
Perlx->can("method")exists x->{field}AUTOLOAD 
Perl 6x.can("method")x.field.definedAUTOLOAD 
OCamlN/A
JavaScripttypeof x.method === "function"field in x  

Special variables

current objectcurrent object's parent objectnull referenceCurrent Context of Execution
SmalltalkselfsupernilthisContext
ABAP Objectsmesuperinitial 
C++ (STL)*this[28]NULL, nullptr 
C#thisbase[29]null 
Javasuper[29] 
D 
JavaScript null, undefined
But be afraid, they have not the same value.
Objective-Cselfsupernil 
Pythonself[30]super(current_class_name, self)[5]
super() (3.x only)
None 
Visual Basic .NETMeMyBaseNothing 
EiffelCurrentPrecursor «{superclass}» «(args)»[29][31]Void 
PHP$thisparent[29]NULL 
Perl$self[30]$self->SUPER[29]undef 
Perl 6selfSUPERNil 
Rubyselfsuper«(args)»[32]nilbinding
Windows PowerShell$this $NULL 
OCamlself[33]super[34]N/A[35] 
F#thisbase[29]null 

Special methods

String representationObject copyValue equalityObject comparisonHash codeObject ID
Human-readableSource-compatible
ABAP ObjectsN/A
C++ (STL)   x == y[36]  pointer to object can be converted into an integer ID
C#x.ToString() x.Clone()x.Equals(y)x.CompareTo(y)x.GetHashCode()System.Runtime.CompilerServices.Runti meHelpers.GetHashCode(x)
Javax.toString() x.clone()[37]x.equals(y)x.compareTo(y)x.hashCode()System.identityHashCode(x)
JavaScriptx.toString()      
Dx.toString() or
std.conv.to!string(x)
x.stringof x == y x.toHash() 
Objective-C (Cocoa)[x description][x debugDescription][x copy][38][x isEqual:y][x compare:y][39][x hash]pointer to object can be converted into an integer ID
Smalltalkx displayStringx printStringx copyx = y x hashx identityHash
Pythonstr(x)[40]repr(x)[41]copy.copy(x)[42]x == y[43]cmp(x, y)[44]hash(x)[45]id(x)
Visual Basic .NETx.ToString() x.Clone()x.Equals(y)x.CompareTo(y)x.GetHashCode() 
Eiffelx.out x.twinx.is_equal(y)When x is COMPARABLE, one can simply do x < yWhen x is HASHABLE, one can simply do x.hash_codeWhen x is IDENTIFIED, one can simply do x.object_id
PHPsprintf("%s", x)[46] clone x[47]x == y  spl_object_hash(x)
Perl"$x"[48]Data::Dumper->Dump([$x],['x'])[49]Storable::dclone($x)[50]   Scalar::Util::refaddr( $x )[51]
Perl 6~x[48]x.perlx.clonex eqv yx cmp y x.WHICH
Rubyx.to_sx.inspectx.dup or
x.clone
x == y or
x.eql?(y)
x <=> yx.hashx.object_id
Windows PowerShellx.ToString() x.Clone()x.Equals(y)x.CompareTo(y)x.GetHashCode() 
OCaml  Oo.copy xx = y Hashtbl.hash xOo.id x
F#string x or x.ToString() or sprintf "%O" xsprintf "%A" xx.Clone()x = y or x.Equals(y)compare x y or x.CompareTo(y)hash x or x.GetHashCode() 

Type manipulation

Get object typeIs instance of (includes subtypes)UpcastingDowncasting
Runtime checkNo check
ABAP ObjectsN/A[52]= ?=
C++ (STL)typeid(x)dynamic_cast<type *>(&x) != NULLN/A[53]dynamic_cast<type*>(ptr)(type*) ptr or
static_cast<type*>(ptr)
C#x.GetType()x is type(type) x or x as type 
Dtypeid(x) cast(type) x 
Delphi x is typex as type 
Javax.getClass()x instanceof class(type) x 
Objective-C (Cocoa)[x class][x isKindOfClass:[class class]] (type*) x
JavaScriptx.constructor (If not rewritten.)x instanceof classN/A[54]
Visual Basic .NETx.GetType()TypeOf x Is typeN/A[53]CType(x, type) or TryCast(x, type) 
Eiffelx.generating_typeattached {TYPE} xattached {TYPE} x as down_x 
Pythontype(x)isinstance(x, type)N/A[54]
PHPget_class(x)x instanceof class
Perlref(x)x->isa("class")
Perl 6x.WHATx.isa(class)N/A[53]type(x) or
x.type
 
Rubyx.classx.instance_of?(type) or
x.kind_of?(type)
N/A[54]
Smalltalkx classx isKindOf: class
Windows PowerShellx.GetType()x -is [type]N/A[53][type]x or x -as [type] 
OCamlN/A[55](x :> type)N/A
F#x.GetType()x :? type(x :?> type) 

Namespace management

Import namespaceImport item
qualifiedunqualified
ABAP Objects   
C++ (STL) using namespace ns;using ns::item ;
C# using ns;using item = ns.item;
D import ns;import ns : item;
Java import ns.*;import ns.item
Objective-C   
Visual Basic .NET Imports ns 
Eiffel   
Pythonimport nsfrom ns import *from ns import item
PHP use ns;use ns\item;
Perluse ns; use ns qw(item);
Perl 6  
Ruby   
Windows PowerShell   
OCaml open ns 
F#  

Contracts

PreconditionPostconditionCheckInvariantLoop
ABAP ObjectsN/A
C++ (STL)
C#Spec#:
type foo( «parameters» )
    requires expression
{
    body
}
Spec#:
type foo( «parameters» )
    ensures expression
{
    body
}
JavaN/A
Objective-C
Visual Basic .NET
Df in { expression } body{ instructions }f out (result) { expression } body{ instructions } invariant() { expression } 
Eiffelf
require tag: expression
do end
f
do
ensure
tag: expression
end
f
do
check tag: expression end
end
class X
invariant tag: expression
end
from instructions
invariant
tag: expression
until
expr
loop
instructions
variant
tag: expression
end
PythonN/A
PHP
Perl
Perl 6PRE { condition }POST { condition }   
RubyN/A
Windows PowerShell
OCaml
F#

See also

References and notes

  1. ^ parameter = argument may be repeated if the constructor has several parameters
  2. ^ SAP reserved to himself the use of destruction
  3. ^ a b c d e f g h i This language uses garbage collection to release unused memory.
  4. ^ OCaml objects can be created directly without going through a class.
  5. ^ a b c d e f g This language supports multiple inheritance. A class can have more than one parent class
  6. ^ Not providing a parent class makes the class a root class. In practice, this is almost never done. One should generally use the conventional base class of the framework one is using, which is NSObject for Cocoa and GNUstep, or Object otherwise.
  7. ^ Usually the @interface portion is placed into a header file, and the @interface portion is placed into a separate source code file.
  8. ^ In Python interfaces are classes whose methods have pass as their bodies
  9. ^ A finalizer 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.
  10. ^ In ABAP, the constructor is to be defined like a method (see comments about method) with the following restrictions: the method name must be "constructor", and only "importing" parameters can be defined
  11. ^ An optional comma-separated list of initializers for member objects and parent classes goes here. The syntax for initializing member objects is "member_name(parameters)" This works even for primitive members, in which case one parameter is specified and that value is copied into the member. The syntax for initializing parent classes is "class_name(parameters)". If an initializer is not specified for a member or parent class, then the default constructor is used.
  12. ^ Any Eiffel procedure can be used as a creation procedure, aka constructors. See Eiffel paragraph at Constructor (computer science).
  13. ^ Implementing {DISPOSABLE}.dispose ensures that dispose will be called when object is garbage collected.
  14. ^ This "initializer" construct is rarely used. Fields in OCaml are usually initialized directly in their declaration. Only when additional imperative operations are needed is "initializer" used. The "parameters to the constructor" in other languages are instead specified as the parameters to the class in OCaml. See the class declaration syntax for more details.
  15. ^ This syntax is usually used to overload constructors
  16. ^ a b c Scope identifier must appear once in the file declaration, all variable declarations after this scope identifier have his scope, until another scope identifier or the end of class declaration is reached
  17. ^ In ABAP, you don't declare specific fields or methods to be accessible by outside things. Rather, you declare outside classes to be friends to have access to the class's fields or methods.
  18. ^ In C++, you don't declare specific fields to be accessible by outside things. Rather, you declare outside functions and classes to be friends to have access to the class's fields. See friend function and friend class for more details.
  19. ^ The declaration and implementation of methods in ABAP are separate. methods statement is to be used inside the class definition. method (without "s") is to be used inside the class implementation. parameter = argument can be repeated if there are several parameters.
  20. ^ In ABAP, the return parameter name is explicitly defined in the method signature within the class definition
  21. ^ The declaration and implementation of methods in C++ are usually separate. Methods are declared inside the class definition (which is usually included in a header file) using the syntax
    type foo(«parameters»);
    The implementation of methods is usually provided in a separate source file, with the following syntax
    type class::foo(«parameters») { instructions }
    Although the body of a method could be included with the declaration inside the class definition, as shown in the table here, this is generally a bad idea. Because the class definition will need to be included with every source file which uses the fields or methods of the class, having code in the class definition will cause the method code to be compiled with every source file, increasing the size of the code. There are some circumstances, however, where it is useful to include the body of a method with the declaration. One reason is that the compiler will try to inline methods that are included in the class declaration; so if you have a very short one-liner method, it may make it faster to allow the compiler to inline it, by including the body along with the declaration. Also, if you have a template class or method, then all the code must be included with the declaration, because only with the code can the template be instantiated.
  22. ^ these examples need the Class::Accessor module installed
  23. ^ a b c Although Eiffel does not support overloading of operators, it can define operators
  24. ^ PHP does not support operator overloading natively, but support can be added using the "operator" PECL package.
  25. ^ Your class needs to implement the ArrayAccess interface.
  26. ^ Your class needs to overload '@{}' (array dereference) or subclass one of Tie::Array or Tie::StdArray to hook array operations
  27. ^ a b In ABAP, arguments must be passed using this syntax:
    x->method(«exporting parameter = argument» «importing parameter = argument» «changing parameter = argument» «returning value(parameter)»
    parameter = argument can be repeated if there are several parameters
  28. ^ C++ doesn't have a "super" keyword, because multiple inheritance is possible, and so it may be ambiguous which base class is desired. Instead, you can use the BaseClassName::member syntax to access an overridden member in the specified base class. Microsoft Visual C++ provides a non-standard keyword "__super" for this purpose; but this is not supported in other compilers.[1]
  29. ^ a b c d e f The keyword here is not a value in itself and it can only be used to access a method of the superclass.
  30. ^ a b In this language, instance methods are passed the current object as the first parameter, which is conventionally named "self", but this is not required to be the case.
  31. ^ "Precursor" in Eiffel is actually a call to the method of the same name in the superclass. So Precursor(args) is equivalent to "super.currentMethodName(args)" in Java. There is no way of calling a method of different name in the superclass.
  32. ^ "super" in Ruby, unlike in other languages, is actually a call to the method of the same name in the superclass. So super(args) in Ruby is equivalent to "super.currentMethodName(args)" in Java. There is no way of calling a method of different name in the superclass.
  33. ^ In OCaml, an object declaration can optionally start with a parameter which will be associated with the current object. This parameter is conventionally named "self", but this is not required to be the case. It is good practice to put a parameter there so that one can call one's own methods.
  34. ^ In OCaml, an inheritance declaration ("inherit") can optionally be associated with a value, with the syntax "inherit parent_class «parameters» as super". Here "super" is the name we gave to the variable associated with this parent object. It can be named something else.
  35. ^ However, if you really wanted the ability to have an "optional" value in OCaml, you would wrap the value inside an option type, whose values are None and Some x, which could be used to represent "null reference" and "non-null reference to an object" as in other languages.
  36. ^ assuming that "x" and "y" are the objects (and not a pointer). Can be customized by overloading the object's == operator
  37. ^ Only accessible from within the class itself, since the clone() method inherited from Object is protected, unless the class overrides the method and makes it public. If you use the clone() inherited from Object, your class will need to implement the Cloneable interface to allow cloning.
  38. ^ Implemented by the object's copyWithZone: method
  39. ^ compare: is the conventional name for the comparison method in Foundation classes. However, no formal protocol exists
  40. ^ Can be customized by the object's __str__() method
  41. ^ Can be customized by the object's __repr__() method
  42. ^ Can be customized by the object's __copy__() method
  43. ^ Can be customized by the object's __eq__() method
  44. ^ Only in Python 2.x and before (removed in Python 3.0). Can be customized by the object's __cmp__() method
  45. ^ Can be customized by the object's __hash__() method. Not all types are hashable (mutable types are usually not hashable)
  46. ^ Can be customized by the object's __toString() method
  47. ^ Can be customized by the object's __clone() method
  48. ^ a b Can be customized by overloading the object's string conversion operator
  49. ^ This example requires useing Data::Dumper
  50. ^ This example requires useing Storable
  51. ^ This example requires useing Scalar::Util
  52. ^ Run-time type information in ABAP can be gathered by using different description Classes like CL_ABAP_CLASSDESCR.
  53. ^ a b c d Upcasting is implicit in this language. A subtype instance can be used where a supertype is needed.
  54. ^ a b c This language is dynamically typed. Casting between types is not necessary.
  55. ^ This language doesn't give run-time type information. It is unnecessary because it is statically typed and downcasting is not possible.
(Sebelumnya) Comparison of programming lang ...Comparison of programming lang ... (Berikutnya)