-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_sample.py
More file actions
92 lines (76 loc) · 2.06 KB
/
class_sample.py
File metadata and controls
92 lines (76 loc) · 2.06 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
80
81
82
83
84
85
86
87
88
89
90
91
92
# class Fruit:
# def __init__(self):
# self.name = 'apple'
# self.color = 'red'
# #example
# my_fruit = Fruit()
# print(my_fruit.name)
# print(my_fruit.color)
# class Fruit:
# def __init__(self, name, color):
# self.name = name
# self.color = color
# self.details()
# def details(self):
# print('my ' + self.name + ' is ' + self.color)
#example
# apple = Fruit('apple', 'red')
# banana = Fruit('banana', 'yellow')
# apple.details()
# inheritance
# class TropicalFruit(Fruit):
# pass
# my_fruit = TropicalFruit('apple', 'red')
#results = my apple is red
# class TropicalFruit(Fruit):
# def taste(self):
# print('my ' + self.color + ' ' + self.name +' tastes good')
# my_fruit = TropicalFruit('apple', 'red')
# my_fruit.taste()
#results = my red apple tastes good
class Fruit:
def __init__(self,name = 'apple'):
self.name = name
self.color = 'red'
self.details()
self.__cost = 10
def details(self):
print('my ' + self.name + ' is ' + self.color)
class TropicalFruit(Fruit):
def __init__(self):
super().__init__(name='banana')
# self.name = 'banana'
self.color ='yellow'
self.__cost = 50
def taste(self):
print('my ' + self.color + ' ' + self.name +' tastes good')
def __secret(self):
print('the fruit cost is actually $', self._Fruit__cost)
class NewFruit(Fruit):
pass
my_fruit = TropicalFruit()
my_fruit.taste()
print('child class fruit: ',my_fruit.name)
print('parent class fruit: ',Fruit().name)
#results
# my apple is red
# my yellow banana tastes good
# child class fruit: banana
# my apple is red
# parent class fruit: apple
print(my_fruit._TropicalFruit__cost)
my_fruit._TropicalFruit__secret()
#results
# my apple is red
# my yellow banana tastes good
# child class fruit: banana
# my apple is red
# parent class fruit: apple
# 50
# the fruit cost is actually $ 10
print(NewFruit('kiwi').name)
#results
#my kiwi is red
print('tropical fruits is ', my_fruit.name)
#results
# tropical fruit is banana