15-03-2019
This commit is contained in:
70
docs/Programmation/Python/class.md
Normal file
70
docs/Programmation/Python/class.md
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user