-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP_Sessin1.py
53 lines (45 loc) · 1.56 KB
/
OOP_Sessin1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# OOP and Classes
# Day 4
# Create a class 'Character'
class Character:
# These are our 'attributes'
def __init__(self, name, position, health):
self.name = name
self.position = position
self.health = health
self.age = 21
# Using 'def' - we create our 'methods'
def how_do_you_feel(self):
if self.health > 10:
print("I'm fine thanks!")
else:
print("Not so good, I'm afraid.")
def where_are_you(self):
print("I'm at {}".format(self.position))
def how_old_are_you(self):
print("I am {} year old".format(self.age))
# Instantiate the object 'me'
#me = Character()
# This is creating an instance of the character class,
# which is assigned to the variable (or object) 'me'
# At the moment, there is not any actual information
# about the object 'me', we have just assigned 'memory'
# for the object
# Instantiate = allocating memory to an object of a class
# Initialise = giving 'information' about the object of a class
#me.name = "Alice Matthews"
alice = Character("Alice", (3,4), 50)
# alice is instantiated as the 'self' using __init__ and then when we access the class
# we pass the information to the attricbutes (name and position)
morgan = Character("Name", (5,4), 7)
# Print the object data for alice and morgan
print(alice)
print(alice.name)
print(alice.health)
print(alice.age)
print(alice.how_do_you_feel())
print(morgan)
print(morgan.name)
print(morgan.health)
print(morgan.age)
print(morgan.how_do_you_feel())