Python,覆盖继承方法的功能
Python, override inherited method's functionality
我有以下 class.
class PlotOnMap:
def __init__(self, lat_lon_a, lat_lon_b, optimal, f_map=None):
self.lat_lon_a = lat_lon_a
self.lat_lon_b = lat_lon_b
self.optimal = optimal
self.f_map = f_map
def create_map(self):
"""
This function creates a map with folium, centered with the location of the three given points.
"""
self.f_map = folium.Map([midlat, midlon], zoom_start=12)
def plot_gdt_and_triangulation(self):
"""
This function plots the locations given by the users with markers and create an heat map of the triangulation.
"""
# plot the gdt's locations.
folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=gdt2,
popup='GDT 2',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
).add_to(self.f_map)
# plot the triangulation (regular and optimal) area
plugins.HeatMap(data=self.lat_lon_a,
).add_to(self.f_map)
plugins.HeatMap(data=self.lat_lon_b,
).add_to(self.f_map)
plugins.HeatMap(data=self.optimal,
gradient={0.65: 'lime'}
).add_to(self.f_map)
我继承了它:
class Plot2ndPosition(PlotOnMap):
def __init__(self, lat_lon_a, lat_lon_b, optimal):
self.plot_on_map = super().__init__(lat_lon_a, lat_lon_b, optimal)
def plot_map(self):
return self.plot_on_map.create_map()
def plot_locations(self):
return self.plot_on_map.plot_gdt_and_triangulation()
唯一的区别是,在 Plot2ndPosition class 中,在第二种方法中,我不想绘制 gdt2 和热图,我只想绘制 gdt1 和 uav (第一个和最后一个标记)。
我考虑过将每个变量分配给一个变量并在继承该方法时以某种方式省略它的方向。
def plot_gdt_and_triangulation(self):
"""
This function plots the locations given by the users with markers and create an heat map of the triangulation.
"""
# plot the gdt's locations.
one = folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
)
one.add_to(self.f_map)
two = folium.Marker(
location=gdt2,
popup='GDT 2',
icon=folium.Icon()
)
two.add_to(self.f_map)
three = folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
)
three.add_to(self.f_map)
# plot the triangulation (regular and optimal) area
trian_plot1 = plugins.HeatMap(data=self.lat_lon_a,
).add_to(self.f_map)
trian_plot2 = plugins.HeatMap(data=self.lat_lon_b,
).add_to(self.f_map)
trian_plot3 = plugins.HeatMap(data=self.optimal,
gradient={0.65: 'lime'}
).add_to(self.f_map)
一个选项是覆盖 Plot2ndPosition
中的 plot_gdt_and_triangulation()
class Plot2ndPosition(PlotOnMap):
def __init__(self, lat_lon_a, lat_lon_b, optimal):
super().__init__(lat_lon_a, lat_lon_b, optimal)
def plot_gdt_and_triangulation(self):
folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
).add_to(self.f_map)
你也可以删除plot_map()
,它只是调用create_map()
。您可以使用 Plot2ndPosition
实例来调用它。如果你有更多的逻辑,你不需要将基础 class 保存为成员,它的所有方法都已经可以从 self
获得
def plot_map(self):
return self.create_map()
我有以下 class.
class PlotOnMap:
def __init__(self, lat_lon_a, lat_lon_b, optimal, f_map=None):
self.lat_lon_a = lat_lon_a
self.lat_lon_b = lat_lon_b
self.optimal = optimal
self.f_map = f_map
def create_map(self):
"""
This function creates a map with folium, centered with the location of the three given points.
"""
self.f_map = folium.Map([midlat, midlon], zoom_start=12)
def plot_gdt_and_triangulation(self):
"""
This function plots the locations given by the users with markers and create an heat map of the triangulation.
"""
# plot the gdt's locations.
folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=gdt2,
popup='GDT 2',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
).add_to(self.f_map)
# plot the triangulation (regular and optimal) area
plugins.HeatMap(data=self.lat_lon_a,
).add_to(self.f_map)
plugins.HeatMap(data=self.lat_lon_b,
).add_to(self.f_map)
plugins.HeatMap(data=self.optimal,
gradient={0.65: 'lime'}
).add_to(self.f_map)
我继承了它:
class Plot2ndPosition(PlotOnMap):
def __init__(self, lat_lon_a, lat_lon_b, optimal):
self.plot_on_map = super().__init__(lat_lon_a, lat_lon_b, optimal)
def plot_map(self):
return self.plot_on_map.create_map()
def plot_locations(self):
return self.plot_on_map.plot_gdt_and_triangulation()
唯一的区别是,在 Plot2ndPosition class 中,在第二种方法中,我不想绘制 gdt2 和热图,我只想绘制 gdt1 和 uav (第一个和最后一个标记)。
我考虑过将每个变量分配给一个变量并在继承该方法时以某种方式省略它的方向。
def plot_gdt_and_triangulation(self):
"""
This function plots the locations given by the users with markers and create an heat map of the triangulation.
"""
# plot the gdt's locations.
one = folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
)
one.add_to(self.f_map)
two = folium.Marker(
location=gdt2,
popup='GDT 2',
icon=folium.Icon()
)
two.add_to(self.f_map)
three = folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
)
three.add_to(self.f_map)
# plot the triangulation (regular and optimal) area
trian_plot1 = plugins.HeatMap(data=self.lat_lon_a,
).add_to(self.f_map)
trian_plot2 = plugins.HeatMap(data=self.lat_lon_b,
).add_to(self.f_map)
trian_plot3 = plugins.HeatMap(data=self.optimal,
gradient={0.65: 'lime'}
).add_to(self.f_map)
一个选项是覆盖 Plot2ndPosition
plot_gdt_and_triangulation()
class Plot2ndPosition(PlotOnMap):
def __init__(self, lat_lon_a, lat_lon_b, optimal):
super().__init__(lat_lon_a, lat_lon_b, optimal)
def plot_gdt_and_triangulation(self):
folium.Marker(
location=gdt1,
popup='GDT 1',
icon=folium.Icon()
).add_to(self.f_map)
folium.Marker(
location=uav,
popup='Target Area.',
icon=folium.Icon()
).add_to(self.f_map)
你也可以删除plot_map()
,它只是调用create_map()
。您可以使用 Plot2ndPosition
实例来调用它。如果你有更多的逻辑,你不需要将基础 class 保存为成员,它的所有方法都已经可以从 self
def plot_map(self):
return self.create_map()