翻訳 : Object-oriented programming - Wikipedia 第1章

 

  • この記事は、コンピュータサイエンスに関する技術および英語についての正確な知識のない翻訳者が個人的な勉強を目的として、ほぼ全てを意訳によって翻訳したために、極めて多くの誤訳が含まれていると想定される決して質が高いとは言えない文章を公開しているものです。ここに掲載されている情報を使用したことによる、読者の方または第三者に生じる損害および損失について、翻訳者は一切の責任を負いません。正しい情報が必要な場合は原文を参照してください。

 

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another.[1][2] There is significant diversity of OOP languages, but the most popular ones are class-based, meaning that objects are instances of classes, which typically also determine their type.

オブジェクト指向プログラミング(OOP)は、オブジェクト、つまり、しばしば属性として知られるフィールドの形でデータを保有し、しばしばメソッドとして知られるプロシージャの形でコードを保有する、「オブジェクト」の概要に基づくプログラミング・パラダイムである。オブジェクトの特徴は、オブジェクトの手順が関連するオブジェクト(オブジェクトには「これ」と「自分」の概念がある)のデータ・フィールドにアクセスして変更できるところにある。オブジェクト指向プログラミングでは、コンピュータプログラミングはほかのオブジェクトと相互に作用するオブジェクトからプログラミングを作り出すことで設計する。オブジェクト指向プログラミング言語には膨大な多様性があるが、最も一般的なものは、オブジェクトがクラス(タイプを決定するもの)のインスタンスであることを意味する、クラス・ベースである。

Many of the most widely used programming languages (such as C++, Object Pascal, Java, Python etc.) are multi-paradigm programming languages that support object-oriented programming to a greater or lesser degree, typically in combination with imperative, procedural programming. Significant object-oriented languages include Java, C++, C#, Python, PHP, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Common Lisp, and Smalltalk.

C++, Object Pascal, Java, Pythonなどのような)最もよく使われる多くのプログラミング言語は、特に命令型プログラミングと手続き型プログラミングを組み合わせて、多かれ少なかれオブジェクト指向プログラミングをサポートするマルチ・パラダイムプログラミング言語である。重要なオブジェクト指向言語Java, C++, C#, Python, PHP, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Common Lisp, Smalltalkである。

 

Features 特徴

Object-oriented programming uses objects, but not all of the associated techniques and structures are supported directly in languages that claim to support OOP. The features listed below are, however, common among languages considered strongly class- and object-oriented (or multi-paradigm with OOP support), with notable exceptions mentioned.[3][4][5][6]

オブジェクト指向言語はオブジェクトを使うが、関連する技術と構造が全てオブジェクト指向プログラミングをサポートとすると言っている言語において直接サポートされているわけではない。しかしながら、次に掲げる特徴は強いクラス指向、オブジェクト指向(または、オブジェクト指向プログラミングをサポートするマルチ・パラダイム)と見られる言語に共通して見られるが、明らかな例外もある。

1.1 Shared with non-OOP predecessor languages オブジェクト指向でないプログラミング言語との共通点

  • Variables that can store information formatted in a small number of built-in data types like integers and alphanumeric characters. This may include data structures like strings, lists, and hash tables that are either built-in or result from combining variables using memory pointers
    変数とは整数やアルファベットのような組込みデータ型の少しだけで作られる情報を持つもの。これは、組込みまたはポインタ変数を合わせてできる、文字列、リスト、ハッシュテーブルのようなデータ構造も含むかもしれない。
  • Procedures – also known as functions, methods, routines, or subroutines – that take input, generate output, and manipulate data. Modern languages include structured programming constructs like loops and conditionals.
    プロシージャは、関数、メソッド、ルーチン、サブルーチンとしても知られ、入出力やデータの操作をする。現代的な現代ではループや分岐のような構造化プログラミングを含む。

Modular programming support provides the ability to group procedures into files and modules for organizational purposes. Modules are namespaced so identifiers in one module will not be accidentally confused with a procedure or variable sharing the same name in another file or module.

 モジュール・プログラミングは手順を組織化するためにファイルやモジュールにまとめる能力の提出をサポートする。あるモジュールの識別子がほかのファイルやモジュール内で同じ名前になるプロシージャと偶然にも混乱させられないようにするためにモジュールは名前空間になっている。

1.2 Objects and classes オブジェクトとクラス

Languages that support object-oriented programming typically use inheritance for code reuse and extensibility in the form of either classes or prototypes. Those that use classes support two main concepts:

オブジェクト指向プログラミングをサポートしている言語は概してコード再利用とクラスとプロトタイプの拡張性ための継承が使える。クラスを使う言語は以下の二つのメインコンセプトをサポートする。

  • Classes – the definitions for the data format and available procedures for a given type or class of object; may also contain data and procedures (known as class methods) themselves, i.e. classes contain the data members and member functions
    クラス–データフォーマットと与えられた型やオブジェクトのクラスが利用できるプロシージャの定義。クラスメソッドとして知られるデータやプロシージャを持つこともある。例えばデータのメンバとメンバ・関数のあるクラス。
  • Objects – instances of classes
    オブジェクト–クラスのインスタンス

Objects sometimes correspond to things found in the real world. For example, a graphics program may have objects such as "circle", "square", "menu". An online shopping system might have objects such as "shopping cart", "customer", and "product".[7] Sometimes objects represent more abstract entities, like an object that represents an open file, or an object that provides the service of translating measurements from U.S. customary to metric.

オブジェクトはよく現実世界のものに対応する。例えば、グラフィックプログラミングでは「円」、「四角形」、「メニュー」といったオブジェクトがあるかもしれない。オンラインショッピングシステムでは「ショッピングカート」、「カスタマー」、「プロダクト」といったオブジェクトがあるかもしれない。ときどきオブジェクトは、オープンファイルをあらわすオブジェクト、もしくはアメリカの慣習的なものからメートル法に測定法を変換するサービスを提供するオブジェクトのようなより抽象的な実体を表す。

Object-oriented programming is more than just classes and objects; it's a whole programming paradigm based around objects (data structures) that contain data fields and methods. It is essential to understand this; using classes to organize a bunch of unrelated methods together is not object orientation.
オブジェクト指向プログラミングは単なるクラスとオブジェクトよりも広い概念だ。それはデータ・フィールドとメソッドを含むオブジェクト(データ構造)に基づくプログラミングパラダイム全てだ。関係のないメソッドの束をまとめるクラスを使うことはオブジェクト指向ではないということを理解することは重要だ。
Junade Ali, Mastering PHP Design Patterns[8]

Each object is said to be an instance of a particular class (for example, an object with its name field set to "Mary" might be an instance of class Employee). Procedures in object-oriented programming are known as methods; variables are also known as fields, members, attributes, or properties. This leads to the following terms:
それぞれのオブジェクトは特定のクラス(例えば、名前のフィールドが「Mary」に設定されたオブジェクトは従業員クラスのインスタンスかもしれない)のインスタンスと言われる。オブジェクト指向言語でのプロシージャはメソッドとして知られる。変数は、フィールド、メンバ、属性、プロパティとしても知られる。これらから次の語句について説明する。

  • Class variables – belong to the class as a whole; there is only one copy of each one
    クラス変数ー全体的にクラスに所属する。一つに対してコピーは一つのみである。
  • Instance variables or attributes – data that belongs to individual objects; every object has its own copy of each one
    インスタンス変数ー個々のオブジェクトに所属するデータ。全てのオブジェクトは全てオブジェクトには各変数の独自のコピーがある。
  • Member variables – refers to both the class and instance variables that are defined by a particular class
    メンバ変数ー特定のクラスによって定義されるクラスとインスタンス変数の両方を指す。
  • Class methods – belong to the class as a whole and have access only to class variables and inputs from the procedure call
    クラスメソッドー全体としてクラスに属してクラス変数にのみアクセスできてプロシージャの呼び出しで入力される
  • Instance methods – belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables
    インスタンスメソッドー個々のオブジェクトに所属して、それらが呼び出される特定のオブジェクトに対するインスタンス変数、入力、クラス変数にアクセスする。

Objects are accessed somewhat like variables with complex internal structure, and in many languages are effectively pointers, serving as actual references to a single instance of said object in memory within a heap or stack. They provide a layer of abstraction which can be used to separate internal from external code. External code can use an object by calling a specific instance method with a certain set of input parameters, read an instance variable, or write to an instance variable. Objects are created by calling a special type of method in the class known as a constructor. A program may create many instances of the same class as it runs, which operate independently. This is an easy way for the same procedures to be used on different sets of data.

オブジェクトは複雑な内部構造をもつ変数のような何かにアクセスする。多くの言語では、ヒープやスタックのオブジェクトと呼ばれる一つのインスタンスに実際に参照する事実上のポインターである。オブジェクトは外部のコードから内ぷを分離するために使われうる抽象化のレイヤーを提供する。外部コードは入力引数リストで特定のインスタンスメソッドを呼び出すことでオブジェクトを使い、インスタンス変数を読み込み、または、インスタンス変数に書き込むことができる。オブジェクトはコンストラクタというクラス内の特殊なメソッドを呼び出して作られる。プログラムは動いているとき同じクラスの多くのインスタンスを作ることもある。それらは独立して動く。これは同じプロシージャが異なるデータセットで使われる簡単な方法だ。

Object-oriented programming that uses classes is sometimes called class-based programming, while prototype-based programming does not typically use classes. As a result, a significantly different yet analogous terminology is used to define the concepts of object and instance.

プロトタイプベースプログラミングは一般にクラスを用いない一方で、クラスを用いるオブジェクト指向プログラミングはときどきクラスベースプログラミングと言われる。その結果、大きく異なるが似た語句がオブジェクトとインスタンスの概念を定義するために使われる。

In some languages classes and objects can be composed using other concepts like traits and mixins.

いくつかの言語ではクラスとオブジェクトはトレイトやmixinのようなほかの概念から成り立ちうる。

1.3 Class-based vs prototype-based クラスベースとプロトタイプベースの比較

In class-based languages the classes are defined beforehand and the objects are instantiated based on the classes. If two objects apple and orange are instantiated from the class Fruit, they are inherently fruits and it is guaranteed that you may handle them in the same way; e.g. a programmer can expect the existence of the same attributes such as color or sugar content or is ripe.

クラスベース言語ではクラスはクラスは前もって定義されて、オブジェクトはクラスに基づいてインスタンス化される。二つのオブジェクト、リンゴとオレンジがフルーツクラスからインスタンス化されると、それらは本質的にフルーツであり、同じように扱うことが保証される。例えば、プログラマーは、色や糖分、または熟しているかといったような属性の存在を期待する。

In prototype-based languages the objects are the primary entities. No classes even exist. The prototype of an object is just another object to which the object is linked. Every object has one prototype link (and only one). New objects can be created based on already existing objects chosen as their prototype. You may call two different objects apple and orange a fruit, if the object fruit exists, and both apple and orange have fruit as their prototype. The idea of the fruit class doesn't exist explicitly, but as the equivalence class of the objects sharing the same prototype. The attributes and methods of the prototype are delegated to all the objects of the equivalence class defined by this prototype. The attributes and methods owned individually by the object may not be shared by other objects of the same equivalence class; e.g. the attributes sugar content may be unexpectedly not present in apple. Only single inheritance can be implemented through the prototype.

プロトタイプベース言語ではオブジェクトは初等のエンティティである。クラスすら存在しない。オブジェクトのプロトタイプはたんなるそのオブジェクトがリンクされているもう一つののオブジェクトである。全てオブジェクトが一つのプロトタイプリンクを持っている。新しいオブジェクトは、プロトタイプとして選択された既に存在するオブジェクトに基づいて作られるだろう。フルーツオブジェクトが存在していると、二つの異なるオブジェクト、リンゴとオレンジを呼び出し、リンゴとオレンジの両方はプロトタイプとしてフルーツを持っている。フルーツクラスの考え方は明らかに存在しないが、同じプロトタイプを共有するオブジェクトの同値クラスとして存在する。プログラミングの属性とメソッドはプロトタイプによって定義された同値クラスのオブジェクトに委譲される。オブジェクトが個々に所有する属性とメソッドは同じ同値クラスの他のオブジェクトが持っていないかもしれない。例えば、属性砂糖はリンゴに予期せず存在しないかもしれない。プロトタイプを通じてたった一つの継承が実装されうる。

1.4 Dynamic dispatch/message passing 動的ディスパッチとメッセージ受け渡し

It is the responsibility of the object, not any external code, to select the procedural code to execute in response to a method call, typically by looking up the method at run time in a table associated with the object. This feature is known as dynamic dispatch, and distinguishes an object from an abstract data type (or module), which has a fixed (static) implementation of the operations for all instances. If there are multiple methods that might be run for a given name, it is known as multiple dispatch.

 実行時にオブジェクトに関連した表の中のメソッドを調べることによる、メソッドコールに反応して実行するために外部コードでなく手順のコードを選ぶことは、オブジェクトの責任である。この特徴は動的ディスパッチと呼ばれ、オブジェクトを、全てのインスタンスを操作することの実装を修正する、抽象データ型(やモジュール)と区別する。与えられた名前で実行されるだろうマルチプルメソッドがあると、マルチプルディスパッチと言われる。

A method call is also known as message passing. It is conceptualized as a message (the name of the method and its input parameters) being passed to the object for dispatch.

メソッドコールはメッセージパッシングとも言われる。ディスパッチのためのオブジェクトへ送る(メソッドの名前と入力引数)メッセージとして概念化される。

1.5 Encapsulation カプセル化

Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

カプセル化はデータとデータを操作する関数を一緒にまとめて、外部のインタフェースと誤用から安全性を維持するためのオブジェクト指向プログラミングの概念である。データのカプセル化はデータ隠蔽の重要なオブジェクト指向プログラミングの概念につながる。

If a class does not allow calling code to access internal object data and permits access through methods only, this is a strong form of abstraction or information hiding known as encapsulation. Some languages (Java, for example) let classes enforce access restrictions explicitly, for example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword. Methods may also be designed public, private, or intermediate levels such as protected (which allows access from the same class and its subclasses, but not objects of a different class). In other languages (like Python) this is enforced only by convention (for example, private methods may have names that start with an underscore). Encapsulation prevents external code from being concerned with the internal workings of an object. This facilitates code refactoring, for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code (as long as "public" method calls work the same way). It also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling.

もしクラスがコードに内部のオブジェクトデータにアクセスさせず、メソッドだけを通してアクセスを許可すれば、これはカプセル化として知られる抽象化や情報隠蔽の強い形である。(Javaなどの)いくつかの言語ではクラスに明確に制限をする。例えばprivate句で内部データを表し、public句でクラス外のコードによる使用を意図するメソッドをデザインする。メソッドはpublic, private, 、protectedのような中間のレベル(同じクラスからはアクセスできるがほかのクラスのオブジェクトからはアクセスできない)がデザインできる。(Pythonのような)ほかの言語では、これはしきたりによってのみ強制される(例えば、private メソッドはアンダースコアから始まる名前)。カプセル化は外部コードがオブジェクトの内部の働きに興味を持たせないようにする。これはリファクタリングを容易にする。例えば、クラスの作者が(publicメソッドが同じ方法で呼び出す限り)外部コードを一切変えずにクラスのオブジェクトの内部データの表し方を変えてもよい。また、プログラマに対して、あるデータの集まりに関連する全てのコードを、ほかのプログラマが簡単に理解できるように作られた、同じクラスに置くことを促す。カプセル化は結合度を下げることを促す技術だ。

1.6 Composition, inheritance, and delegation 集約、継承、委譲

Objects can contain other objects in their instance variables; this is known as object composition. For example, an object in the Employee class might contain (point to) an object in the Address class, in addition to its own instance variables like "first_name" and "position". Object composition is used to represent "has-a" relationships: every employee has an address, so every Employee object has a place to store an Address object.

オブジェクトはインスタンス変数に他のオブジェクトを持つことができる。これはオブジェクト集約として知られる。例えば、従業員オブジェクトは、「名前」や「役職」といったインスタンス変数を所有することに加えて、アドレスクラスのオブジェクトを持つ(指す)だろう。オブジェクト集約は「has-a」関係を表す。つまり、全ての従業員は住所を持っているので全ての従業員クラスにはアドレスオブジェクトを持つ場所がある。

Languages that support classes almost always support inheritance. This allows classes to be arranged in a hierarchy that represents "is-a-type-of" relationships. For example, class Employee might inherit from class Person. All the data and methods available to the parent class also appear in the child class with the same names. For example, class Person might define variables "first_name" and "last_name" with method "make_full_name()". These will also be available in class Employee, which might add the variables "position" and "salary". This technique allows easy re-use of the same procedures and data definitions, in addition to potentially mirroring real-world relationships in an intuitive way. Rather than utilizing database tables and programming subroutines, the developer utilizes objects the user may be more familiar with: objects from their application domain.[9]

クラスをサポートする言語はほとんどいつも継承をサポートする。これによってクラスは「is-a-type-of」関係を表す階層構造に整えられる。例えば、従業員クラスは人クラスを継承するかもしれない。親クラスを利用できる全てのデータとメソッドは同じ名前の子クラスに表示される。例えば、人クラスはmake_full_name()メソッドと変数first_name, last_nameをを定義するかもしれない。これらは「役職」と「給料」を追加しているかもしれない従業員クラスでも利用できるだろう。この技術を使えば直感的な方法で現実世界の関係を潜在的に映し出すことに加えて、同じ手順とデータの定義を簡単に再利用できる。データベースの表を利用してサブルーチンをプログラミングするよいrも、開発者はユーザーがより親しんでいる、応用分野に由来するオブジェクトを利用する。

Subclasses can override the methods defined by superclasses. Multiple inheritance is allowed in some languages, though this can make resolving overrides complicated. Some languages have special support for mixins, though in any language with multiple inheritance, a mixin is simply a class that does not represent an is-a-type-of relationship. Mixins are typically used to add the same methods to multiple classes. For example, class UnicodeConversionMixin might provide a method unicode_to_ascii() when included in class FileReader and class WebPageScraper, which don't share a common parent.

サブクラスはスーパークラスで定義されたメソッドをオーバライドできる。多重継承は、リゾルビングオーバライドを複雑にするだろうが、いくつかの言語で利用できる。いくつかの言語では、多重継承が使える全ての言語で、mixinは「is-a-type-of」関係を表さない単純なクラスだが、mixinの特別なサポートがある。mixinはとくに多重クラスに同じメソッドを追加するために使われる。例えば、UnicodeConversionMixinクラスはunicode_to_ascii() を、親を共有しないFileReaderクラスとWebPageScraperクラスに含まれている時に、提供するかもしれない。

Abstract classes cannot be instantiated into objects; they exist only for the purpose of inheritance into other "concrete" classes which can be instantiated. In Java, the final keyword can be used to prevent a class from being subclassed.

抽象クラスはオブジェクトへとインスタンス化することができない。インスタンス化できるほかの「具象」クラスに継承させるためだけに存在する。Javaでは、final句はクラスにサブクラスを持たせないことができる。

The doctrine of composition over inheritance advocates implementing has-a relationships using composition instead of inheritance. For example, instead of inheriting from class Person, class Employee could give each Employee object an internal Person object, which it then has the opportunity to hide from external code even if class Person has many public attributes or methods. Some languages, like Go do not support inheritance at all.

継承を超えた集約のドクトリンは継承の代わりに集約をつかったhas-a関係を実装することである。例えば、人クラスから継承する代わりに従業員クラスは、もし人クラスがpublic属性やメソッドを持っているときですら外部コードから隠す機会を持っている、内部人クラスを従業員オブジェクトに与える。GOのようないくつかの言語では継承は全くサポートされていない。

The "open/closed principle" advocates that classes and functions "should be open for extension, but closed for modification".

開放/閉鎖原則はクラスと関数は「拡張に対して開いていて、修正に対して閉じているべき」というものだ。

Delegation is another language feature that can be used as an alternative to inheritance.

委譲は継承を代替することができるほかの言語特徴だ。

1.7 Polymorphism ポリモーフィズム

Subtyping, a form of polymorphism, is when calling code can be agnostic as to whether an object belongs to a parent class or one of its descendants. For example, a function might call "make_full_name()" on an object, which will work whether the object is of class Person or class Employee. This is another type of abstraction which simplifies code external to the class hierarchy and enables strong separation of concerns.

派生型は、ポリモーフィズムの形態で、呼び出すコードが親クラスまたはその子孫に属していないオブジェクトであるかどうかを知らないときにいう。例えば、関数は人クラスか従業員クラスのオブジェクトであるか考えるオブジェクトについての「make_full_name()」を呼ぶかもしれない。クラス階層の外部コードを単純化し関心の分離を強化する抽象型の一つである

1.8 Open recursion

In languages that support open recursion, object methods can call other methods on the same object (including themselves), typically using a special variable or keyword called this or self. This variable is late-bound; it allows a method defined in one class to invoke another method that is defined later, in some subclass thereof.

open recursionをサポートする言語では、オブジェクトメソッドは特に自身で呼ばれる特別な変数やキーワードを使って(自身を含む)同じオブジェクトについてのほかのメソッドを呼び出すことができる。この変数は束縛変数、つまり、あるクラスで定義されるメソッドであとから定義するほかのメソッドを呼び出すことを可能にする。