-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
executable file
·79 lines (58 loc) · 1.77 KB
/
hello.py
File metadata and controls
executable file
·79 lines (58 loc) · 1.77 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import random
import sys
import os
#Lesson 10 - Objects/classes
print("\n"*10)
class Animal:
name = "" #underscores make the attribute private
height = 0 #to changes these values you need to use a funcition
weight = 0 #in the class
sound = 0
#constructor
def __init__(self, name, height, weight, sound):
self.name = name
self.height = height
self.weight = weight
self.sound = sound
#setters
def set_name(self, name): #self lets class to refer to itself
self.name = name
def set_height(self, height):
self.height =height
def set_weight(self, weight):
self.weight= weight
def set_sound(self, sound):
self.sound = sound
#getters
def get_name(self):
return self.name
def get_height(self):
return self.height
def get_weight(self):
return self.weight
def get_sound(self):
return self.sound
def get_type(self):
print("Animal")
def toString(self):
return "{} is {} cm tall {} kilograms and say {}".format(self.name,
self.height,
self.weight,
self.sound)
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())
#inheritance - making another class with a class and inheriting all it's attributes
class Dog(Animal):
def __init__(self, name, height, weight, sound, owner):
super(Dog, self).__init__(name, height, weight, sound)
self.ownerName = owner
def set_owner(self, owner):
self.ownerName = owner
def get_owner(self):
return self.ownerName
def get_type(self):
print("Dog")
def toString(self):
return "{} is {} cm tall {} kilograms and says {} and his owner is {}".format(self.name, self.height,self.weight,self.sound, self.ownerName)
charlie = Dog('charlie', 20, 30, 'woof', 'Emmanuel')
print(charlie.toString())