瀏覽代碼

Update python-07.md

master
Dimitri Merejkowsky 5 年之前
父節點
當前提交
01e0643dd2
共有 1 個文件被更改,包括 55 次插入0 次删除
  1. +55
    -0
      sessions/python-07.md

+ 55
- 0
sessions/python-07.md 查看文件

@@ -0,0 +1,55 @@
% Programmation avec Python (chapitre 7)
% Dimitri Merejkowsky


#

\center \huge Rappels sur les classes


# Définition d'une classe

Construire la classe`Counter` avec un attribut `count`:

```python
class Counter:
def __init__(self):
self.count = 0
```



# Instantiation

Construire une nouvelle *instance* de `Counter`

```python
>>> counter = Counter()
>>> counter.count
0
```

# Méthode

Ajouter une méthode pour incrémenter le compteur:

```python
class Counter:
def __init__(self):
self.count = 0

def increment(self):
self.count += 1

```

# Apeller une méthode

```python
>>> counter = Counter()
>>> counter.count
0
>>> counter.increment()
>>> counter.count
1
```