-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_class.py
161 lines (88 loc) · 2.73 KB
/
my_class.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 1 09:52:12 2020
@author: wangmeiqi
"""
#Reference https://www.learncodewithmike.com/2020/01/python-inheritance.html
import numpy as np
import pandas as pd
grades = {
"name": ["Mike", "Sherry", "Cindy", "John"],
"math": [80, 75, 93, 86],
"chinese": [63, 90, 85, 70]
}
class MyDataFrame(pd.DataFrame): #Inherit
def identity_mapping(self):
return self
result = MyDataFrame(grades)
print(isinstance(result, MyDataFrame)) #判斷物件與類別的關係
##############################################################################
#Inheritance
#DRY don't repeat yourself
#交通工具 base class
class Transportation:
#constructor
def __init__(self):
self.color = "white" #attribution
#駕駛方法
def drive(self):
print("Drive method is called")
#car's subclass
class Car(Transportation):
#method
def accelerate(self):
print("accelerate method is called")
#airplane's subclass
class Airplane(Transportation):
#method
def fly(self):
print("fly method is called")
toyota = Car()
toyota.drive() #print
toyota.accelerate()
plane = Airplane()
plane.fly() #print
##############################################################################
#Method overriding
#base class and sub class have the same name of method
class Bike(Transportation):
def drive(self):
print("sub method is called")
bicycle = Bike()
bicycle.drive() #print
#opertate the method in the base class
class Motocycle(Transportation):
def drive(self):
super().drive()
print("sub method is called")
moto = Motocycle()
moto.drive() #print
##############################################################################
#Multi-Level Inheritance
#繼承(Inheritance)在程式碼的重用(Reusable)上非常好
#但是如果沒有適當的使用就會像此範例一樣產生邏輯上的錯誤(鴨子不會飛)
#建議不超過兩層,否則反而會增加程式碼的複雜度及難以維護
class Animal:
pass
class Bird(Animal):
def fly(self):
print("fly!!!")
class Duck(Bird):
pass
duck = Duck()
duck.fly()
##############################################################################
#Multiple Inheritance
#須避免個廢別裡有相同名稱之方法
class Animal:
def walk(self):
print("it can walk")
class Bird:
def eat(self):
print("it can eat")
class Duck(Animal, Bird):
pass
duck = Duck()
duck.walk()
duck.eat()