プログラミング言語の比較(オブジェクト指向プログラミング)
このプログラミング言語の比較では、C++、Java、Smalltalk、Object Pascal、Perl、Pythonなどのオブジェクト指向プログラミング言語がデータ構造をどのように操作するかを比較します
オブジェクトの構築と破壊
| 構築 | 破壊 | |
|---|---|---|
| ABAPオブジェクト | data variable type ref to class .[1] | [2] [3] |
| APL(ダイアログ) | variable←⎕NEW class «parameters» | ⎕EX 'variable' |
| C++ | class variable«(parameters)»;[4]または[5]class* variable = new class«(parameters)»; | delete pointer; |
| C# | class variable = new class(parameters); | variable.Dispose();[3] |
| Java | [3] | |
| D | destroy(variable); | |
| eC | class «instance handle» { «properties/data members assignments, instance method overrides» } | delete instance handle; |
| Objective-C ( Cocoa ) | class *variable = [[class alloc ] init];またはclass *variable = [[class alloc ] initWithFoo:parameter «bar:parameter ...»]; | [variable release]; |
| Swift | let variable = class(parameters) | |
| Python | variable = class(parameters) | del variable[3](通常は必要ありません) |
| Visual Basic .NET | Dim variable As New class(parameters) | variable.Dispose()[3] |
| Xojo | Dim variable As New class(parameters) | variable = Nil |
| エッフェル | create variableまたはまたはまたはcreate «{TYPE}» variable.make_foo «(parameters)»variable := create {TYPE}variable := create {TYPE}.make_foo «(parameters)» | [3] |
| PHP | $variable = new class«(parameters)»; | unset($variable);[3] |
| Perl 5 | «my »$variable = class->new«(parameters)»; | undef($variable); |
| 楽 | «my »$variable = class.new«(parameters)»; | $variable.undefine; |
| Ruby | variable = class.new«(parameters)» | [3] |
| Windows PowerShell | $variable = New-Object «-TypeName» class ««-ArgumentList» parameters» | Remove-Variable «-Name» variable |
| OCaml | let variable = new class «parameters»または[6]let variable = object members end | [3] |
| F# | let variable = «new »class(«parameters») | |
| Smalltalk | クラスはオブジェクトです。 クラスにメッセージを送信するだけです。通常は #newまたは ですが#new:、他にも多くの種類があります。例:ポイント x: 10 y: 20 。配列 with: -1 with: 3 with: 2 。 | |
| JavaScript | var variable = new class«(parameters)»またはvar variable = { «key1: value1«, key2: value2 ...»»} | [3] |
| Object Pascal ( Delphi ) | ClassVar := ClassType.ConstructorName(parameters); | ClassVar.Free; |
| Scala | val obj = new Object // パラメータなしval obj = new Object ( arg0 , arg1 , arg2 ...) val obj = Object ( arg0 , arg1 , arg2 ...) // ケースクラスval obj = new Object ( arg0 , arg1 , param1 = value1 , ...) // 名前付きパラメータ | [3] |
| COBOL | INVOKE class "NEW" RETURNING variableまたはMOVE class::"NEW" TO variable | |
| Cobra | variable «as class» = class(parameters) | variable.dispose |
| ISLISP | (setq variable (create (class <some-class> [:field-1 value-1 [:field-2 value-2] ..]))) | [3] |
クラス宣言
| クラス | プロトコル | 名前空間 | |
|---|---|---|---|
| ABAPオブジェクト | class name definition «inheriting from parentclass». «interfaces: interfaces.» method_and_field_declarations endclass. | interface name. members endinterface. | — |
| APL (Dyalog) | :Class name «:parentclass» «,interfaces»members:EndClass | :Interface namemembers:EndInterface | :Namespace namemembers:EndNamespace |
| C++ | class name« : public parentclasses[7]» { members }; | namespace name { members } | |
| C# | class name« : «parentclass»«, interfaces»» { members } | interface name« : parentinterfaces» { members } | |
| D | module name; | ||
| eC | class name« : base class» { «default member values assignments» «members» } | | namespace name; |
| Java | class name« extends parentclass»« implements interfaces» { members } | interface name« extends parentinterfaces» { members } | package name; members |
| PHP | namespace name; members | ||
| Objective-C | @interface name« : parentclass»[8] [9]«< protocols >» { instance_fields } method_and_property_declarations @end | @protocol name«< parentprotocols >» members @end | [10] |
| Swift | class name« : «parentclass»«, protocols»» { members } | protocol name« : parentprotocols» { members } | |
| Python | class name«(parentclasses[7])»: | [11] | __all__ = [ member1,member2,... ] |
| Visual Basic .NET | Class name« Inherits parentclass»« Implements interfaces» | Interface name« Inherits parentinterfaces» | Namespace name |
| Xojo | Class name« Inherits parentclass»« Implements interfaces» | Interface name« Inherits parentinterfaces» | Module name |
| エッフェル | class name« inherit parentclasses[7]» | — | |
| パール | package name; «@ISA = qw(parentclasses[7]);» members 1; | package name; members | |
| 楽 | class name «is parentclass «is parentclass ...[7]»» «does role «does role ...»» { members } | role name «does role «does role ...»» { members } | module name { members } |
| ルビー | class name« < parentclass» | module name | |
| Windows PowerShell | — | ||
| OCaml | class name «parameters» = object «(self)» «inherit parentclass «parameters» «inherit parentclass «parameters» ...[7]»» members end | module name | |
| F# | type name«(parameters)» «as this» = class «inherit parentclass«(parameters)» «as base»» members «interface interface with implementation «interface interface with implementation ...»» end | type name = interface members end | namespace name |
| Smalltalk | [12] | [13] | |
| JavaScript (ES6) | class name «extends parentclass» { members } | ||
| Object Pascal (Delphi) | ClassName = Class «( ClassParent, Interfaces) » | package name; members | |
| Scala | クラスConcreteClass (コンストラクタパラメータ)はParentClassを継承し、Trait1とTrait2とTrait2 { // メンバー} | 特性TraitName はOtherTrait1とOtherTrait2とOtherTrait3を拡張します{ // メンバー} | パッケージ名 |
| COBOL | CLASS-ID. name« INHERITS« FROM» parentclasses».FACTORY« IMPLEMENTS interfaces».class-membersEND FACTORY.OBJECT« IMPLEMENTS interfaces».instance-membersEND OBJECT.
| INTERFACE-ID. name« INHERITS« FROM» interfaces».members
| — |
| コブラ | class name «inherits parentclass» «implements interfaces» | interface name «inherits parentinterfaces» | namespace name |
| ISLISP | (defclass name (base-class) ((x :initform 0 :accessor get-x :initarg x)) (:abstractp nil)) | ||
クラスメンバー
コンストラクタとデストラクタ
| constructor | デストラクタ | ファイナライザ[14] | |
|---|---|---|---|
| ABAP Objects | methods constructor «importing parameter = argument»[15] | — | |
| APL (Dyalog) | ∇ name:Implements Constructor «:Base «expr»»instructions∇ | ∇ name:Implements Destructorinstructions∇ | |
| C++ | class(«parameters») «: initializers[16]» { instructions } | ~class() { instructions } | |
| C# | class(«parameters») { instructions } | void Dispose(){ instructions } | ~class() { instructions } |
| D | this(«parameters») { instructions } | ~this() { instructions } | |
| eC | class() { instructions } | ~class() { instructions } | |
| Java | class(«parameters») { instructions } | void finalize() { instructions } | |
| Eiffel | [17] | [18] | |
| Objective-C(Cocoa) | | | |
| Swift | init(«parameters») { instructions } | deinit { instructions } | |
| Python | | | |
| Visual Basic .NET | | | |
| Xojo | Sub Constructor(«parameters») | Sub Destructor() | |
| PHP | | | |
| Perl | | | |
| Raku | | | |
| Ruby | def initialize«(parameters)» | — | |
| Windows PowerShell | — | ||
| OCaml | initializer instructions[19] | — | |
| F# | do instructions or[20] | | |
| JavaScript | function name(«parameters») { instructions }[21] | — | |
| JavaScript (ES6) | constructor(«parameters») { instructions } | ||
| COBOL | — [22] | — | |
| Cobra | cue init(parameters) | def dispose | |
| ISLISP | (defmethod initialize-object ((instance <class-name>) initvalues) | ||
フィールド
| 公開 | 非公開 | 保護 | 友達 | |
|---|---|---|---|---|
| ABAP Objects | public section.[23] data field type type. | private section.[23] data field type type. | protected section.[23] data field type type. | [24] |
| APL (Dyalog) | :Field Public field «← value» | :Field «Private» field «← value» | ||
| C++ | public: type field; | private: type field; | protected: type field; | [25] |
| C# | public type field «= value»; | private type field «= value»; | protected type field «= value»; | internal type field «= value»; |
| D | package type field «= value»; | |||
| Java | protected type field «= value»; | type field «= value»; | ||
| eC | public type field; | private type field; | ||
| Eiffel | feature | feature {NONE} | feature {current_class} | feature {FRIEND} |
| Objective-C | @public type field; | @private type field; | @protected type field; | @package type field; |
| Swift | — | |||
| Smalltalk | — | [26] | — | |
| Python | self.field = value[27] | — [28] | — | |
| Visual Basic .NET | Public field As type «= value» | Private field As type «= value» | Protected field As type «= value» | Friend field As type «= value» |
| Xojo | Public field As type «= value» | Private field As type «= value» | Protected field As type «= value» | — |
| PHP | public $field «= value»; | private $field «= value»; | protected $field «= value»; | |
| Perl | $self->{field} = value;[27] | — | ||
| Raku | has« type »$.field« is rw» | has« type »$!field | — | |
| Ruby | — | @field = value[27] | ||
| Windows PowerShell | Add-Member | — | ||
| OCaml | — | val «mutable» field = value | — | |
| F# | — | let «mutable» field = value | — | |
| JavaScript | this.field = value [27] | |||
| COBOL | — | レベル番号フィールド句[ 29] | — | — |
| Cobra | var field «as type» «= value» | var __field «as type» «= value» | var _field «as type» «= value» | |
| ISLISP | (field :initform value :accessor accessor-name :initarg keyword) | |||
方法
| 基本メソッド/voidメソッド | 値を返すメソッド | ||
|---|---|---|---|
| ABAP Objects | methods name «importing parameter = argument» «exporting parameter = argument» «changing parameter = argument» «returning value(parameter)»[30] | [31] | |
| APL (Dyalog) | ∇ «left argument» name «right arguments»instructions∇ | ∇ result ← «left argument» name «right arguments»instructions∇ | |
C++ [32]
メソッドの実装は通常、次の構文で別のソースファイルで提供されます
| void foo(«parameters») { instructions } | type foo(«parameters») { instructions ... return value; } | |
| C# | |||
| D | |||
| Java | |||
| eC | void ««type of 'this'»::»foo(«parameters») { instructions } | type ««type of this»::»foo(«parameters») { instructions ... return value; } | |
| Eiffel | foo ( «parameters» ) | foo ( «parameters» ): TYPE | |
| Objective-C | - (void)foo«:parameter «bar:parameter ...»» { instructions } | - (type)foo«:parameter «bar:parameter ...»» { instructions... return value; } | |
| Swift | func foo(«parameters») { instructions } | func foo(«parameters») -> type { instructions... return value } | |
| Python | def foo(self«, parameters»): | def foo(self«, parameters»): | |
| Visual Basic .NET | Sub Foo(«parameters») | Function Foo(«parameters») As type | |
| Xojo | Sub Foo(«parameters») | Function Foo(«parameters») As type | |
| PHP | function foo(«parameters»)«: void» { instructions } | function foo(«parameters»)«: type» { instructions ... return value; } | |
| Perl | sub foo { my ($self«, parameters») = @_; instructions } | sub foo { my ($self«, parameters») = @_; instructions ... return value; } | |
| Raku | «has »«multi »method foo(««$self: »parameters») { instructions } | «has «type »»«multi »method foo(««$self: »parameters») { instructions ... return value; } | |
| Ruby | def foo«(parameters)» | def foo«(parameters)» | |
| Windows PowerShell | Add-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions } -InputObject variable | Add-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions ... return value } -InputObject variable | |
| OCaml | — | method foo «parameters» = expression | |
| F# | member this.foo(«parameters») = expression | ||
| JavaScript | this.method = function(«parameters») {instructions} [34] | this.method = function(«parameters») {instructions... return value;} [34] | |
| Javascript (ES6) | foo(«parameters») {instructions} | foo(«parameters») {instructions... return value;} | |
| COBOL | METHOD-ID. foo.instructions
| METHOD-ID. foo.instructions
| |
| Cobra | def foo(parameters) | def foo(parameters) as type | |
| ISLISP | (defgeneric method (arg1 arg2)) |
プロパティ
「Bar」という名前のプロパティを宣言する方法
手動で実装
| 読み書き | 読み取り専用 | 書き込み専用 | |
|---|---|---|---|
| ABAP Objects | — | ||
| APL (Dyalog) | :Property Bar∇ result ← Getinstructions∇∇ Set argumentsinstructions∇:EndProperty Bar | :Property Bar∇ result ← Getinstructions∇:EndProperty Bar | :Property Bar∇ Set argumentsinstructions∇:EndProperty Bar |
| C++ | — | ||
| C# | type Bar { | type Bar { get { instructions ... return value; } } | type Bar { set { instructions } } |
| D | @property type bar() { instructions ... return value; } | @property type bar() { instructions ... return value; } | @property type bar(type value) { instructions ... return value; } |
| eC | property type Bar { | property type Bar { get { instructions ... return value; } } | property type Bar { set { instructions } } |
| Java | — | ||
| Objective-C 2.0 (Cocoa) | @property (readwrite) type bar; そして内部@implementation | @property (readonly) type bar; and then inside @implementation | — |
| Swift | var bar : type { get { instructions } set«(newBar)» { instructions } } | var bar : type { instructions } | — |
| Eiffel | feature -- Access | ||
| Python | def setBar(self, value): [35] | def getBar(self): | def setBar(self, value): |
| Visual Basic .NET | Property Bar() As type | ReadOnly Property Bar() As type | WriteOnly Property Bar() As type |
| Xojo | ComputedProperty Bar() As type | ComputedProperty Bar() As type | ComputedProperty Bar() As type |
| PHP | function __get($property) { | function __get($property) { | function __set($property, $value) { |
| Perl | sub Bar { | sub Bar { | sub Bar { |
| Raku | — | ||
| Ruby | def bar | def bar | def bar=(value) |
| Windows PowerShell | Add-Member | Add-Member | Add-Member |
| OCaml | — | ||
| F# | member this.Bar with get() = expression and set(value) = expression | member this.Bar = expression | member this.Bar with set(value) = expression |
| JavaScript (ES6) | get bar(«parameters») { instructions ... return value}set bar(«parameters») { instructions } | get bar(«parameters») { instructions ... return value} | set bar(«parameters») { instructions } |
| COBOL | METHOD-ID. GET PROPERTY bar.instructions
instructions
| METHOD-ID. GET PROPERTY bar.instructions
| METHOD-ID. SET PROPERTY bar.instructions
|
| Cobra | pro bar «as type» | get bar «as type» | set bar «as type» |
| ISLISP | — | ||
自動的に実装
| read-write | read-only | write-only | |
|---|---|---|---|
| ABAP Objects | — | ||
| C++ | — | ||
| C# | type Bar { get; set; } | type Bar { get; private set; } | type Bar { private get; set; } |
| D | — | ||
| Java | — | ||
| Objective-C 2.0 (Cocoa) | @property (readwrite) type bar; そして中へ@implementation | @property (readonly) type bar; and then inside @implementation | — |
| Swift | var bar : type | let bar : type | — |
| Eiffel | |||
| Python | @property | @property | bar = property() |
| Visual Basic .NET | Property Bar As type« = initial_value» (VB 10) | ||
| PHP | |||
| Perl [36] | use base qw(Class::Accessor); | use base qw(Class::Accessor); | use base qw(Class::Accessor); |
| Raku | — | ||
| Ruby | attr_accessor :bar | attr_reader :bar | attr_writer :bar |
| Windows PowerShell | |||
| OCaml | — | ||
| F# | member val Bar = value with get, set | ||
| COBOL | level-number bar clauses PROPERTY. | level-number bar clauses PROPERTY «WITH» NO SET. | level-number bar clauses PROPERTY «WITH» NO GET. |
| Cobra | pro bar from var «as type» | get bar from var «as type» | set bar from var «as type» |
オーバーロードされた演算子
標準演算子
| 単項 | 二項 | 関数呼び出し | |
|---|---|---|---|
| ABAP Objects | — | ||
| C++ | type operator symbol () { instructions } | type operator symbol (type operand2) { instructions } | type operator () («parameters») { instructions } |
| C# | static type operator symbol(type operand) { instructions } | static type operator symbol(type operand1, type operand2) { instructions } | — |
| D | type opUnary(string s)() if (s == "symbol") { instructions } | type opBinary(string s)(type operand2) if (s == "symbol") { instructions } | type opCall(«parameters») { instructions } |
| Java | — | ||
| Objective-C | |||
| Swift | func symbol(operand1 : type) -> returntype { instructions }(クラス外) | func symbol(operand1 : type1, operand2 : type2) -> returntype { instructions } (outside class) | |
| エッフェル[37] | op_name alias "symbol": TYPE | op_name alias "symbol" (operand: TYPE1): TYPE2 | |
| Python | def __opname__(self): | def __opname__(self, operand2): | def __call__(self«, parameters»): |
| Visual Basic .NET | Shared Operator symbol(operand As type) As type | Shared Operator symbol(operand1 As type, operand2 As type) As type | — |
| Xojo | Function Operator_name(operand As type) As type | — | |
| PHP | [38] | function __invoke(«parameters») { instructions } (PHP 5.3+) | |
| Perl | use overload "symbol" | use overload "symbol" | |
| Raku | «our «type »»«multi »method prefix:<symbol> («$operand: ») { instructions ... return value; } or | «our «type »»«multi »method infix:<symbol> («$operand1: » type operand2) { instructions ... return value; } | «our «type »»«multi » |
| Ruby | def symbol | def symbol(operand2) | — |
| Windows PowerShell | — | ||
| OCaml | |||
| F# | static member (symbol) operand = expression | static member (symbol) (operand1, operand2) = expression | — |
| COBOL | — | ||
| ISLISP | — | ||
インデクサ
| read-write | read-only | write-only | |
|---|---|---|---|
| ABAP Objects | — | ||
| APL (Dyalog) | :Property Numbered Default name∇ result ← Getinstructions∇∇ Set argumentsinstructions∇:EndProperty Bar | :Property Numbered Default Bar∇ result ← Getinstructions∇:EndProperty Bar | :Property Numbered Default Bar∇ Set argumentsinstructions∇:EndProperty Bar |
| C++ | type& operator[](type index) { instructions } | type operator[](type index) { instructions } | |
| C# | type this[type index] { | type this[type index] { get{ instructions } } | type this[type index] { set{ instructions } } |
| D | type opIndex(type index) { instructions } | type opIndex(type index) { instructions } | type opIndexAssign(type value, type index) { instructions } |
| Java | — | ||
| Objective-C(最近のClangコンパイラ) | — | | |
| Swift | subscript (index : type) -> returntype { get { instructions } set«(newIndex)» { instructions } } | subscript (index : type) -> returntype { instructions } | |
| Eiffel[37] | bracket_name alias "[]" (index: TYPE): TYPE assign set_item | bracket_name alias "[]" (index: TYPE): TYPE | |
| Python | def __getitem__(self, index): | def __getitem__(self, index): | def __setitem__(self, index, value): |
| Visual Basic .NET | | | |
| PHP | [39] | ||
| Perl | [40] | ||
| Raku | «our «type »»«multi » | «our «type »»«multi » | — |
| Ruby | def [](index) | def [](index) | def []=(index, value) |
| Windows PowerShell | — | ||
| OCaml | |||
| F# | | | |
| COBOL | — | ||
| Cobra | pro[index «as type»] as type | get[index «as type»] as type | set[index «as type»] as type |
タイプキャスト
| ダウンキャスト | アップキャスト | |
|---|---|---|
| ABAP Objects | — | |
| C++ | 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 } | |
| eC | property T { get { return «conversion code»; } } | |
| Java | — | |
| Objective-C | ||
| Eiffel[37] | ||
| Python | ||
| Visual Basic .NET | Shared Narrowing Operator CType(operand As type) As returntype | Shared Widening Operator CType(operand As type) As returntype |
| PHP | — | |
| Perl | ||
| Raku | multi method type«($self:)» is export { instructions } | |
| Ruby | — | |
| Windows PowerShell | ||
| OCaml | ||
| F# | ||
| COBOL | — | |
メンバーアクセス
オブジェクトxのメンバーにアクセスする方法
| オブジェクトメンバー | クラスメンバー | 名前空間メンバー | |||
|---|---|---|---|---|---|
| メソッド | フィールド | 物件 | |||
| ABAP Objects | x->method(«parameters»).[41] | x->field | — | x=>field or x=>method(«parameters[41]»). | — |
| C++ | x.method(parameters) or | x.field or | cls::member | ns::member | |
| Objective-C | [x method«:parameter «bar:parameter ...»»] | x->field | x.property (2.0 only) or | [cls method«:parameter «bar:parameter ...»»] | |
| Smalltalk | x method«:parameter «bar:parameter ...»» | — | cls method«:parameter «bar:parameter ...»» | ||
| Swift | x.method(parameters) | x.property | cls.member | ||
| APL (Dyalog) | left argument» x.method «right argument(s)» | x.field | x.property | cls.member | ns.member |
| C# | x.method(parameters) | ||||
| Java | — | ||||
| D | x.property | ||||
| Python | |||||
| Visual Basic .NET | |||||
| Xojo | |||||
| Windows PowerShell | [cls]::member | ||||
| F# | — | cls.member | |||
| eC | x.method«(parameters)» | x.field | x.property | cls::member | ns::member |
| Eiffel | x.method«(parameters)» | x.field | {cls}.member | — | |
| Ruby | — | x.property | cls.member | ||
| PHP | x->method(parameters) | x->field | x->property | cls::member | ns\member |
| Perl | x->method«(parameters)» | x->{field} | cls->method«(parameters)» | ns::member | |
| Raku | x.method«(parameters)» or | x.field or | cls.method«(parameters)» or | ns::member | |
| OCaml | x#method «parameters» | — | |||
| JavaScript | x.method(parameters) | x.field | x.property | cls.member | — |
| COBOL | INVOKE x "method" «USING parameters» «RETURNING result» or | — | property OF x | INVOKE cls "method" «USING parameters» «RETURNING result» or | — |
| Cobra | x.method«(parameters)» | x.field | x.property | cls.member | ns.member |
会員の空き状況
| 会員はいますか? | 欠落したメンバーのハンドラ | |||
|---|---|---|---|---|
| メソッド | フィールド | Method | Field | |
| APL (Dyalog) | 3=x.⎕NC'method' | 2=x.⎕NC'method' | — | |
| ABAP Objects | — | |||
| C++ | ||||
| Objective-C (Cocoa) | [x respondsToSelector:@selector(method)] | — | forwardInvocation: | — |
| Smalltalk | x respondsTo: selector | — | doesNotUnderstand: | — |
| C# | (リフレクションを使用) | |||
| eC | ||||
| Java | ||||
| D | opDispatch() | |||
| Eiffel | — | |||
| Python | hasattr(x, "method") and callable(x.method) | hasattr(x, "field") | __getattr__() | |
| Visual Basic .NET | (using reflection) | |||
| Xojo | (イントロスペクションを使用) | |||
| Windows PowerShell | (using reflection) | |||
| F# | (using reflection) | |||
| Ruby | x.respond_to?(:method) | — | method_missing() | — |
| PHP | method_exists(x, "method") | property_exists(x, "field") | __call() | __get() / __set() |
| Perl | x->can("method") | exists x->{field} | オートロード | |
| Raku | x.can("method") | x.field.defined | AUTOLOAD | |
| OCaml | — | |||
| JavaScript | typeof x.method === "function" | field in x | ||
| COBOL | — | |||
特殊変数
| 現在のオブジェクト | 現在のオブジェクトの親オブジェクト | null参照 | 現在の実行コンテキスト | |
|---|---|---|---|---|
| Smalltalk | self | super | nil | thisContext |
| ABAP Objects | me | super | initial | |
| APL (Dyalog) | ⎕THIS | ⎕BASE | ⎕NULL | |
| C++ | *this | [42] | NULL, nullptr | |
| C# | this | base[43] | null | |
| Java | super[43] | |||
| D | ||||
| JavaScript | super[43](ECMAScript 6) | null, undefined[44] | ||
| eC | this | null | ||
| Objective-C | self | super[43] | nil | |
| Swift | self | super[43] | nil[45] | |
| Python | self[46] | super(current_class_name, self)[7]super() (3.xのみ) | None | |
| Visual Basic .NET | Me | MyBase | Nothing | |
| Xojo | Me / Self | Parent | Nil | |
| エッフェル | Current | Precursor «{superclass}» «(args)»[43] [47] | Void | |
| PHP | $this | parent[43] | null | |
| パール | $self[46] | $self->SUPER[43] | undef | |
| 楽 | self | SUPER | Nil | |
| ルビー | self | super«(args)»[48] | nil | binding |
| Windows PowerShell | $this | $NULL | ||
| OCaml | self[49] | super[50] | — [51] | |
| F# | this | base[43] | null | |
| COBOL | SELF | SUPER | NULL | |
| コブラ | this | base | nil |
特殊メソッド
| 文字列表現 | オブジェクトのコピー | 値の等価性 | オブジェクト比較 | ハッシュコード | オブジェクトID | ||
|---|---|---|---|---|---|---|---|
| 人間が読める形式 | ソース互換 | ||||||
| ABAPオブジェクト | — | ||||||
| APL (Dyalog) | ⍕x | ⎕SRC x | ⎕NS x | x = y | — | ||
| C++ | x == y[52] | オブジェクトへのポインタは整数IDに変換できます | |||||
| C# | x.ToString() | x.Clone() | x.Equals(y) | x.CompareTo(y) | x.GetHashCode() | System | |
| Java | x.toString() | x.clone()[53] | x.equals(y) | x.compareTo(y)[54] | x.hashCode() | System | |
| JavaScript | x.toString() | ||||||
| D | x.toString() or | x.stringof | x == y or | x.opCmp(y) | x.toHash() | ||
| eC | x.OnGetString(tempString, null, null) or | y.OnCopy(x) | x.OnCompare(y) | オブジェクトハンドルは整数IDに変換できる | |||
| Objective-C(Cocoa) | x.description | x.debugDescription | [x copy][55] | [x isEqual:y] | [x compare:y][56] | x.hash | オブジェクトへのポインタは整数IDに変換できます |
| Swift | x.description[57] | x.debugDescription[58] | x == y[59] | x < y[60] | x.hashValue[61] | reflect(x) | |
| Smalltalk | x displayString | x printString | x copy | x = y | x hash | x identityHash | |
| Python | str(x)[62] | repr(x)[63] | copy.copy(x)[64] | x == y[65] | cmp(x, y)[66] | hash(x)[67] | id(x) |
| Visual Basic .NET | x.ToString() | x.Clone() | x.Equals(y) | x.CompareTo(y) | x.GetHashCode() | ||
| エッフェル | x.out | x.twin | x.is_equal(y) | x が のときCOMPARABLE、次のように記述できますx < y | xが のときHASHABLE、x.hash_code | xが のときIDENTIFIED、x.object_id | |
| PHP | $x->__toString() | clone x[68] | x == y | | spl_object_hash(x) | ||
| パール | "$x"[69] | Data::Dumper[70] | Storable[71] | Scalar[72] | |||
| 楽 | ~x[69] | x.perl | x.clone | x eqv y | x cmp y | x.WHICH | |
| ルビー | x.to_s | x.inspect | x.dup or | x == y or | x <=> y | x.hash | x.object_id |
| Windows PowerShell | x.ToString() | x.Clone() | x.Equals(y) | x.CompareTo(y) | x.GetHashCode() | ||
| OCaml | Oo.copy x | x = y | Hashtbl | Oo.id x | |||
| F# | string x or x | sprintf "%A" x | x.Clone() | x = y or x | compare x y or x | hash x or x | |
| COBOL | — | ||||||
型操作
| オブジェクト型を取得 | (サブタイプを含む)のインスタンスである | アップキャスト | ダウンキャスト | ||
|---|---|---|---|---|---|
| 実行時チェック | チェックなし | ||||
| ABAPオブジェクト | — [73] | = | ?= | ||
| C++ | typeid(x) | dynamic_cast<type *>(&x) != nullptr | — [74] | dynamic_cast<type*>(ptr) | (type*) ptr or |
| C# | x.GetType() | x is type | (type) x or x as type | ||
| D | typeid(x) | cast(type) x | |||
| デルファイ | x is type | x as type | |||
| eC | x._class | eClass_IsDerived(x._class, type) | (type) x | ||
| Java | x.getClass() | x instanceof class | (type) x | ||
| Objective-C(Cocoa) | [x class][75] | [x isKindOfClass:[class class]] | (type*) x | ||
| Swift | x.dynamicType | x is type | x as! type x as? type | ||
| JavaScript | x.constructor (If not rewritten.) | x instanceof class | — [76] | ||
| Visual Basic .NET | x.GetType() | TypeOf x Is type | — [74] | CType(x, type) or TryCast(x, type) | |
| Xojo | Introspection.GetType(x) | x IsA type | — | CType(x, type) | — |
| エッフェル | x.generating_type | attached {TYPE} x | attached {TYPE} x as down_x | ||
| Python | type(x) | isinstance(x, type) | — [76] | ||
| PHP | get_class(x) | x instanceof class | |||
| パール | ref(x) | x->isa("class") | |||
| 楽 | x.WHAT | x.isa(class) | — [74] | type(x) or | |
| ルビー | x.class | x.instance_of?(type) or | — [76] | ||
| Smalltalk | x class | x isKindOf: class | |||
| Windows PowerShell | x.GetType() | x -is [type] | — [74] | [type]x or x -as [type] | |
| OCaml | — [77] | (x :> type) | — | ||
| F# | x.GetType() | x :? type | (x :?> type) | ||
| COBOL | — | x AS type[74] | — | ||
名前空間管理
| 名前空間のインポート | アイテムのインポート | ||
|---|---|---|---|
| 修飾子 | 無資格 | ||
| ABAPオブジェクト | |||
| C++ | 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 | ||
| エッフェル | |||
| Python | import ns | from ns import * | from ns import item |
| PHP | use ns; | use ns\item; | |
| パール | use ns; | use ns qw(item); | |
| 楽 | |||
| ルビー | |||
| Windows PowerShell | |||
| OCaml | open ns | ||
| F# | |||
| COBOL | — | ||
契約
| 前提条件 | 事後条件 | チェック | 不変 | ループ | |
|---|---|---|---|---|---|
| ABAPオブジェクト | — | ||||
| C++ | |||||
| C# | Spec#: | Spec#: | |||
| Java | — | ||||
| Objective-C | |||||
| Visual Basic .NET | |||||
| D | f | f | assert(expression) | invariant() { expression } | |
| エッフェル | f | f | f | class X | from instructions |
| Python | — | ||||
| PHP | |||||
| パール | |||||
| 楽 | PRE { condition } | POST { condition } | |||
| ルビー | — | ||||
| Windows PowerShell | |||||
| OCaml | |||||
| F# | |||||
| COBOL | |||||
参照
注記
- コンストラクタに複数のパラメータがある場合、パラメータ = 引数は繰り返すことができます
- ^ SAPは破壊行為を自らに留保した
- ^ abcdefghijkl この言語では、ガベージ コレクションを使用して未使用のメモリを解放します。
- ^ この構文は、自動保存期間を持つオブジェクト値を作成します
- ^ この構文は、動的記憶期間を持つオブジェクトを作成し、それへのポインタを返します。
- ^ OCaml オブジェクトはクラスを経由せずに直接作成できます。
- ^ abcdefg この言語は多重継承をサポートしています。クラスは複数の親クラスを持つことができます。
- ^ 親クラスを指定しない場合、そのクラスはルートクラスになります。実際には、このようなことはほとんど行われません。通常は、使用しているフレームワークの標準的な基底クラス(
NSObjectCocoaやGNUstepなど)を使用するべきですObject。 - ^ 通常、部分はヘッダー ファイル
@interfaceに配置され、部分は別のソース コード ファイルに配置されます。@interface - ^ クラス名やプロトコル名の接頭辞は、慣例的に名前空間の一種として使用される
- ^ Pythonでは、インターフェースはメソッドの本体としてパスを持つクラスです
- ^ クラスはオブジェクトです。
スーパークラス(st-80)または宛先名前空間(Visualworks)にメッセージを送信するだけです。 - ^ 名前空間はオブジェクトです。
親名前空間にメッセージを送信するだけです。 - ^ ファイナライザは、オブジェクトがガベージコレクションされる直前にガベージコレクタによって呼び出されます。ファイナライザがいつ呼び出されるか、あるいは呼び出されるかどうかは保証されません。
- ^ ABAPでは、コンストラクタはメソッドのように定義されます(メソッドに関するコメントを参照)。ただし、メソッド名は「コンストラクタ」でなければならず、「インポート」パラメータのみを定義できます。
- ^ メンバーオブジェクトと親クラスの初期化子をコンマで区切って記述します(省略可能)。メンバーオブジェクトの初期化構文は次のとおりです。
"member_name(parameters)"
"class_name(parameters)".
- ^ 任意のEiffel手続きは、生成手続き(コンストラクタ)として使用できます。コンストラクタ(コンピュータサイエンス)のEiffelの段落を参照してください。
- ^ {DISPOSABLE}.disposeを実装すると、オブジェクトがガベージ コレクションされるときにdispose が呼び出されるようになります。
- ^ この「初期化子」構造はほとんど使用されません。OCamlのフィールドは通常、宣言時に直接初期化されます。追加の命令型操作が必要な場合にのみ、「初期化子」が使用されます。他の言語における「コンストラクタへのパラメータ」は、OCamlではクラスへのパラメータとして指定されます。詳細については、クラス宣言構文を参照してください。
- ^ この構文は通常、コンストラクタをオーバーロードするために使用されます
- ^ JavaScript では、コンストラクターはオブジェクトです。
- ^ コンストラクターは、クラスインスタンスを返すファクトリーメソッドを使用してエミュレートできます。
- ^ abc スコープ識別子はファイル宣言で一度だけ出現する必要があり、このスコープ識別子以降のすべての変数宣言は、別のスコープ識別子またはクラス宣言の終わりに達するまで、そのスコープを持ちます。
- ^ ABAPでは、特定のフィールドやメソッドが外部からアクセス可能と宣言されることはありません。外部クラスはフレンドとして宣言され、そのクラスのフィールドやメソッドにアクセスできるようになります。
- ^ C++では、特定のフィールドを外部からアクセス可能に宣言することはできません。代わりに、外部の関数やクラスをフレンドとして宣言することで、そのクラスのフィールドにアクセスできるようになります。詳細については、フレンド関数とフレンドクラスを参照してください。
- ^ クラスにメッセージを送るだけ
クラス addInstVarName: フィールド。クラス removeInstVarName: フィールド。
- ^ abcd メソッドで値を割り当てるだけです
- ^ Pythonにはプライベートフィールドはありません。すべてのフィールドは常にパブリックアクセス可能です。実装の詳細の前にアンダースコアを1つ付けるというコミュニティの慣習がありますが、言語側では強制されていません。
- ^ COBOL 標準ではクラス データへのアクセス方法が指定されていないため、すべてのクラス データは「プライベート」です。
- ^ ABAP ではメソッドの宣言と実装は別々です。methodsステートメントはクラス定義内で使用します。method ( "s" なし) はクラス実装内で使用します。parameter = argument は、複数のパラメータがある場合に繰り返すことができます。
- ^ ABAPでは、戻りパラメータ名はクラス定義内のメソッドシグネチャで明示的に定義されます。
- ^ C++では、メソッドの宣言と実装は通常は別々です。メソッドはクラス定義(通常はヘッダーファイルにインクルードされます)で以下の構文を使用して宣言されます。
- ^ この表に示すように、 メソッドの本体をクラス定義内の宣言に含めることはできますが、これは一般的に悪い習慣です。クラス定義は、そのクラスのフィールドやメソッドを使用するすべてのソース ファイルに含める必要があるため、クラス定義にコードがあると、メソッドのコードがすべてのソース ファイルでコンパイルされ、コードのサイズが大きくなります。ただし、状況によっては、メソッドの本体を宣言に含めることが有効な場合があります。理由の 1 つは、コンパイラがクラス宣言に含まれているメソッドをインライン化しようとするためです。そのため、非常に短い1 行のメソッドがある場合は、宣言と一緒に本体を含めることで、コンパイラがそれをインライン化できるようにした方が高速化される可能性があります。また、テンプレートクラスまたはメソッドがある場合は、コードがないとテンプレートをインスタンス化できないため、すべてのコードを宣言に含める必要があります。
- ^ ab メソッド内で関数を割り当てるだけです
- ^ 代替実装:
def bar (): doc = "barプロパティ。" def fget ( self ): return self . _bar def fset ( self , value ): self . _bar = value return locals () bar = property ( ** bar ())
- ^ これらの例ではClass::Accessorモジュールがインストールされている必要があります
- ^ abc Eiffelは演算子のオーバーロードをサポートしていないが、演算子を定義することはできる。
- ^ PHP はネイティブでは演算子のオーバーロードをサポートしていませんが、「operator」PECL パッケージを使用してサポートを追加できます。
- ^ クラスは ArrayAccess インターフェイスを実装する必要があります。
- ^ クラスは、配列操作をフックするために、次のいずれかをオーバーロード
@{}(配列の逆参照)またはサブクラス化する必要があります。Tie::ArrayTie::StdArray - ^ ab ABAP では、引数は次の構文を使用して渡す必要があります。
x->method(«exporting parameter = argument» «importing parameter = argument» «changing parameter = argument» «returning value(parameter)»parameter = argument複数のパラメータがある場合は繰り返すことができます
- ^ C++には「super」キーワードがありません。これは多重継承が可能であるため、どの基底クラスを参照しているかが曖昧になる可能性があるためです。代わりに、
BaseClassName::member指定された基底クラス内のオーバーライドされたメンバーにアクセスするための構文を使用できます。Microsoft Visual C++では、この目的のために非標準キーワード「__super」が用意されていますが、これは他のコンパイラではサポートされていません。[1] - ^ abcdefghi ここでのキーワードは値ではなく、スーパークラスのメソッドにアクセスするためにのみ使用できます。
- ^ しかし、心配してください。それらは同じ価値を持っていません。
- ^ オプション型のみ
- ^ ab この言語では、インスタンス メソッドには最初のパラメーターとして現在のオブジェクトが渡されます。このオブジェクトは通常「self」という名前ですが、必ずしもそうである必要はありません。
- ^ Eiffelにおける「Precursor」は、実際にはスーパークラス内の同名のメソッドへの呼び出しです。つまり、Precursor(args)はJavaにおける「super.currentMethodName(args)」と同等です。スーパークラス内の異なる名前のメソッドを呼び出す方法はありません。
- ^ Rubyにおける「super」は、他の言語とは異なり、実際にはスーパークラス内の同名のメソッドの呼び出しです。つまり、Rubyにおける「super(args)」は、Javaにおける「super.currentMethodName(args)」と同等です。スーパークラス内の異なる名前のメソッドを呼び出す方法はありません。
- ^ OCamlでは、オブジェクト宣言はオプションで、現在のオブジェクトに関連付けられるパラメータで始めることができます。このパラメータは慣例的に「self」という名前が付けられますが、必ずしもそうである必要はありません。独自のメソッドを呼び出すために、パラメータをselfに置くことは良い習慣です。
- ^ OCamlでは、継承宣言("inherit")は、構文「 inherit parent_class «parameters» as super」を用いて、オプションで値を関連付けることができます。ここで「super」は、この親オブジェクトに関連付けられた変数に付けられた名前です。別の名前を付けることもできます。
- ^ ただし、OCaml で「オプション」の値を持つ機能が必要な場合は、値を
optionと の型内にラップします。これらの値はNone、Some x他の言語と同様に「null 参照」と「オブジェクトへの非 null 参照」を表すために使用できます。 - ^ 「x」と「y」はオブジェクト(ポインタではない)であると仮定します。オブジェクトの
==演算子をオーバーロードすることでカスタマイズできます。 - ^ から継承したメソッドはprotectedであるため、クラス内でのみアクセス可能です。ただし、クラスがメソッドをオーバーライドしてpublicにした場合は除きます。から継承したメソッドを使用する場合は、クラスはクローン作成を可能にするインターフェースを実装する必要があります。
clone()Objectclone()ObjectCloneable - ^ このメソッドを標準化するには、クラスでインターフェースを実装する必要があります。
Comparable - ^ オブジェクトの
copyWithZone:メソッドによって実装される - ^
compare:は、Foundationクラスの比較メソッドの慣例的な名称です。ただし、正式なプロトコルは存在しません。 - ^ オブジェクトが
Printableプロトコルに準拠している場合のみ - ^ オブジェクトが
DebugPrintableプロトコルに準拠している場合のみ - ^ オブジェクトが
Equatableプロトコルに準拠している場合のみ - ^ オブジェクトが
Comparableプロトコルに準拠している場合のみ - ^ オブジェクトが
hashValueプロトコルに準拠している場合のみ - ^ オブジェクトの
__str__()メソッドによってカスタマイズ可能 - ^ オブジェクトの
__repr__()メソッドによってカスタマイズ可能 - ^ オブジェクトの
__copy__()メソッドによってカスタマイズ可能 - ^ オブジェクトの
__eq__()メソッドによってカスタマイズ可能 - ^ Python 2.x以前でのみ使用可能(Python 3.0では削除)。オブジェクトの
__cmp__()メソッドでカスタマイズ可能 - ^ オブジェクトの
__hash__()メソッドによってカスタマイズできます。すべての型がハッシュ可能というわけではありません(変更可能な型は通常ハッシュ可能ではありません)。 - ^ オブジェクトの
__clone()メソッドによってカスタマイズ可能 - ^ ab オブジェクトの文字列変換演算子をオーバーロードすることでカスタマイズできます
- ^ この例では、 Data::Dumper を使用する必要があります
- ^ この例ではStorable を使用する必要があります
- ^ この例ではScalar::Utilの使用が必要です
- ^ ABAP の実行時型情報は、CL_ABAP_CLASSDESCR などのさまざまな説明クラスを使用して収集できます。
- ^ abcde この言語ではアップキャストが暗黙的に行われます。スーパータイプが必要な場所ではサブタイプのインスタンスを使用できます。
- ^ 非クラスオブジェクトのみ。
xがクラスオブジェクトの場合、[x class]のみを返しますx。ランタイムメソッドは、すべてのオブジェクトobject_getClass(x)のクラスを返します。x - ^ abc この言語は動的型付けです。型間のキャストは不要です。
- ^ この言語は実行時の型情報を提供しません。静的型付けであり、ダウンキャストが不可能であるため、実行時の型情報は不要です。