Classes & objects · クラス & オブジェクト
A class is a blueprint
- A class defines a new kind of thing — a user-defined type.
- An object is one item made from that class.
- One
Dogclass can make many dog objects, each with its own data.
クラスとは設計図
- クラスは新しい種類のものを定義します——ユーザー定義型です。
- オブジェクトはそのクラスから作られた1つのアイテムです。
- 1つの
Dogクラスから多くの犬のオブジェクトを作ることができ、それぞれ固有のデータを持ちます。
init and attributes
__init__is a special method that runs when you make an object.selfrefers to the object being made.- Values stored on
selfare the object's attributes (its data).
__init__と属性
__init__はオブジェクトを作成した際に実行される特殊メソッドです。selfは作成中のオブジェクトを参照します。self上に格納される値はオブジェクトの属性(データ)です。
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(2, 5)
print(p.x)
print(p.y)
Methods
- A method is a function defined inside a class.
- Its first parameter is always
self, the object it works on. - Call it with a dot:
object.method().
メソッド
- メソッドはクラス内で定義された関数です。
- 最初のパラメータは常に
selfであり、これが動作対象のオブジェクトです。 - ドットを使って呼び出します:
object.method()。
class Greeter:
def __init__(self, name):
self.name = name
def hello(self):
return "Hi, " + self.name
g = Greeter("Sam")
print(g.hello())
Enumerated types
- An enumerated type is a fixed set of named values.
from enum import Enumlets you define one as a class.- It is clearer than using loose numbers like
1,2,3.
列挙型
- 列挙型は固定された名の付き値のセットです。
from enum import Enumはクラスとして定義することを許容します。1,2,3のような緩い数字を使用するよりも明瞭です。
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.GREEN)
print(Color.GREEN.value)
In Cambridge pseudocode
- A
CLASSblock lists its data and aNEWconstructor.
Cambridge擬似コードにおける表現
CLASSブロックはデータをリストアップし、NEWコンストラクタを含みます。
CLASS Point
PRIVATE X : INTEGER
PRIVATE Y : INTEGER
PUBLIC PROCEDURE NEW(startX : INTEGER, startY : INTEGER)
X ← startX
Y ← startY
ENDPROCEDURE
ENDCLASS
DECLARE p : Point
p ← NEW Point(2, 5)
Common mistakes
- Every method takes
selfas its first parameter. __init__runs automatically when you create an object.
よくあるミス
- 全てのメソッドは最初のパラメータとして
selfを取ります。 __init__はオブジェクトを作成した際に自動的に実行されます。
Now you try
- Define each class with
__init__and any methods it needs. - Press Check answer to test your code.
あなたも試してみよう
__init__で各クラスを定義し、必要なメソッドを含めます。- 回答を確認 を押してコードを試してください。
Define a class Dog whose __init__ takes name and age and stores them as attributes self.name and self.age. · クラス Dog を定義し、その __init__ は name と age を受け取り、属性 self.name と self.age として格納する。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Define a class Rectangle with __init__(self, width, height) and a method area(self) that returns width * height. · クラス Rectangle を定義し、__init__(self, width, height) を属性として持ち、area(self) というメソッドで width * height を 返す よう実装してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Define a class BankAccount. __init__ sets self.balance to 0. Add deposit(self, amount) to add money and withdraw(self, amount) to take it away. · クラス BankAccount を定義する。__init__ は self.balance を 0 に設定する。deposit(self, amount) を追加して金を増やし、withdraw(self, amount) を追加して金を減らす。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。