-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathChapter_9th_Inheritance_Fruit_example.py
53 lines (48 loc) · 1.38 KB
/
Chapter_9th_Inheritance_Fruit_example.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 6 15:51:01 2020
@author: sana.rasheed
"""
### Chapter 9th - Inheritance
# Example 15
# Fruit class defined as parent
class Fruit:
"""
A class is defined for fruits,
to create objects of this class..
"""
def __init__(self, name, nutrients):
try:
assert type(nutrients) == list
except AssertionError:
print('invalid construction')
raise Exception
self.name = name
self.nutrients = nutrients
self.is_ripe = False
def get_name(self):
return self.name
def get_nutrients (self):
print(self.name + ' has following nutrients:')
for value in self.nutrients:
print (value)
def check_ripeness(self):
return self.is_ripe
def ripe_fruit(self):
self.is_ripe = True
# End of Fruit class
# Citrus is an Inherited Class of Fruit
class Citrus(Fruit):
def __init__(self, name, nutrients):
super().__init__( name,nutrients)
self.type= 'Citrus'
self.characteristic = 'Pulpy and Juicy'
def get_type(self):
return self.type
# End of Citrus class
Orange = Citrus(name='Orange', nutrients=['vitamin D','vitamin C'])
Orange.get_nutrients()
# Output
# Orange has following nutrients:
# vitamin D
# vitamin C