71 lines
768 B
Markdown
71 lines
768 B
Markdown
|
|
|
|
# class
|
|
|
|
|
|
|
|
##### Créer une class:
|
|
|
|
```python
|
|
class MaClass:
|
|
x = 5
|
|
```
|
|
|
|
##### Créer un objet:
|
|
|
|
```python
|
|
p1 = MaClass()
|
|
print(p1.x)
|
|
|
|
5
|
|
```
|
|
|
|
##### Fonction \__init__()
|
|
|
|
La fonction \__init__() est appelée automatiquement chaque fois que la class est utilisée.
|
|
|
|
```python
|
|
class Person:
|
|
def __init__(self, name, age):
|
|
self.name = name
|
|
self.age = age
|
|
|
|
p1 = Person("John", 36)
|
|
|
|
print(p1.name)
|
|
print(p1.age)
|
|
|
|
John
|
|
36
|
|
```
|
|
|
|
##### Méthodes:
|
|
|
|
```python
|
|
class Person:
|
|
def __init__(self, name, age):
|
|
self.name = name
|
|
self.age = age
|
|
|
|
def myfunc(self):
|
|
print("Hello my name is " + self.name)
|
|
|
|
p1 = Person("John", 36)
|
|
p1.myfunc()
|
|
|
|
Hello my name is John
|
|
```
|
|
|
|
##### Modifier les propiétés de l'objet:
|
|
|
|
```python
|
|
p1.age = 40
|
|
```
|
|
|
|
|
|
|
|
```python
|
|
|
|
```
|
|
|