# 基类
class OvanEconomy:
#类属性
ovan_type = "OVAN_ECONOMY"
def __init__(self):
# 实例属性
self.bake_time = 0
self.temperature = 0
def set_baketime(self, bake_time):
self.bake_time = bake_time
def get_baketime(self):
return self.bake_time
def set_temperature(self, temperature):
self.temperature = temperature
def get_temperature(self):
return self.temperature
def bake(self):
print("--------------------------")
print(" %s"%(self.__class__.ovan_type))
print("--------------------------")
print("bake_time: %d s"%(self.bake_time))
print("temperature:", self.temperature)
print("BAKEING......")
# 派生类/基类
class OvanAdvanced(OvanEconomy):
# 类属性
ovan_type = "OVAN_ADVANCED"
def __init__(self):
# 实例属性
super().__init__()
self.temperature_up = 0
self.temperature_down = 0
# 重载
def set_temperature(self, temperature_up, temperature_down):
self.temperature_up = temperature_up
self.temperature_down = temperature_down
# 重载
def get_temperature(self):
return self.temperature_up, self.temperature_down
# 重载
def bake(self):
print("--------------------------")
print(" %s" % (self.__class__.ovan_type))
print("--------------------------")
print("bake_time: %d s"%(self.bake_time))
print("temperature: UP-%d, DOWN-%d"%(self.temperature_up, self.temperature_down))
print("BAKEING......")
# 派生类
class OvanAppointment(OvanAdvanced):
# 类属性
ovan_type = "OVAN_APPOINTMENT"
def __init__(self):
# 实例属性
super().__init__()
self.appointment_time = 0
# 重载
def set_appointment_time(self, appointment_time):
self.appointment_time = appointment_time
# 重载
def get_appointment_time(self):
return self.appointment_time
# 重载
def bake(self):
print("--------------------------")
print(" %s" % (self.__class__.ovan_type))
print("--------------------------")
print("bake_time: %d s"%(self.bake_time))
print("appointment_time: run after %d hours" % (self.appointment_time))
print("temperature: UP-%d, DOWN-%d"%(self.temperature_up, self.temperature_down))
print("BAKEING......")
# 派生类
class OvanWindRotary(OvanAdvanced):
# 类属性
ovan_type = "OVAN_WIND_ROTARY"
def __init__(self):
# 实例属性
super().__init__()
self.wind_rotary_flag = False
# 新增
def on_wind_rotary(self):
self.wind_rotary_flag = True
# 新增
def off_wind_rotary(self):
self.wind_rotary_flag = False
# 重载
def bake(self):
print("--------------------------")
print(" %s" % (self.__class__.ovan_type))
print("--------------------------")
print("bake_time: %d s"%(self.bake_time))
print("temperature: UP-%d, DOWN-%d"%(self.temperature_up, self.temperature_down))
if self.wind_rotary_flag:
print("BAKEING WITH WIND ROTARY......")
else:
print("BAKEING......")
# 测试1:高级烤箱
ovan_advanced = OvanAdvanced()
ovan_advanced.set_baketime(100)
ovan_advanced.set_temperature(100, 200)
ovan_advanced.bake()
# 测试2:高级烤箱+预约功能
ovan_appointment = OvanAppointment()
ovan_appointment.set_baketime(100)
ovan_appointment.set_appointment_time(5)
ovan_appointment.set_temperature(100, 200)
ovan_appointment.bake()
# 测试3:高级烤箱+热风旋转烤
ovan_windrotary = OvanWindRotary()
ovan_windrotary.set_baketime(100)
ovan_windrotary.set_temperature(100, 200)
ovan_windrotary.on_wind_rotary()
ovan_windrotary.bake()
输出
--------------------------
OVAN_ADVANCED
--------------------------
bake_time: 100 s
temperature: UP-100, DOWN-200
BAKEING......
--------------------------
OVAN_APPOINTMENT
--------------------------
bake_time: 100 s
appointment_time: run after 5 hours
temperature: UP-100, DOWN-200
BAKEING......
--------------------------
OVAN_WIND_ROTARY
--------------------------
bake_time: 100 s
temperature: UP-100, DOWN-200
BAKEING WITH WIND ROTARY......
# 基类
class OvanEconomy:
#类属性
ovan_type = "OVAN_ECONOMY"
#类私有属性
__test1 = "TEST1"
def __init__(self):
# 实例属性
self.bake_time = 0
#实例私有属性
self.__test2 = "TEST2"
# 派生类/基类
class OvanAdvanced(OvanEconomy):
# 类属性
ovan_type = "OVAN_ADVANCED"
def __init__(self):
# 实例属性
self.bake_time = 200
super().__init__()
# 生成实例
ovan_advanced = OvanAdvanced()
# 打印实例能访问的属性和方法
print(dir(ovan_advanced))
# 访问父类的类私有属性,实例私有属性
print(ovan_advanced._OvanEconomy__test1, ovan_advanced._OvanEconomy__test2)
输出:
[‘_OvanEconomy__test1‘, ‘_OvanEconomy__test2‘, ‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘, ‘bake_time‘, ‘ovan_type‘]
TEST1 TEST2
# 基类
class base_1:
nick_name = "农场"
#父类初始化
def __init__(self, city):
print("父类初始化开始......")
self.class_type = "base"
self.__class_id = 0
# 这里在实际运行中调用是子类重写的同名方法。
self.produce_id()
self.city = city
def print_id(self):
print("CLASS %s ‘s ID: %d"%(self.__class__.__name__, self.__class_id))
# 派生类,单继承
class derived_1(base_1):
nick_name = "蔬菜基地"
# 初始化重写
def __init__(self, city):
print("子类初始化开始......")
# 采用super调用父类的初始化函数
super(derived_1, self).__init__(city)
# 声明子类自身的__class_id
self.__class_id = 0
print("子类初始化结束!")
# 重写__class_id的打印方法
def print_id(self):
print("CLASS %s ‘s ID: %d"%(self.__class__.__name__, self.__class_id))
# 重写__class_id生成方法
def produce_id(self):
print("我是子类重写的produce_id()")
self.__class_id = random.randint(100000,999999)
def test(self):
print("调用父类的__class_id打印方法打印父类的私有属性:")
super(derived_1, self).print_id()
输出:
# 单继承用例----------------------------------
子类初始化开始......
父类初始化开始......
我是子类重写的produce_id()
父类初始化结束!
子类初始化结束!
调用父类的__class_id打印方法打印父类的私有属性:
CLASS derived_1 ‘s ID: 0 # 这个值并未如期望中的改变,因为父类初始化中调用的是子类重写的produce_id方法。
分析:父类初始化时调用了produce_id()方法,但是调用的是子类重写的方法,因此父类的用例私有属性__class_id并未更新,实际上该调用生成了子类的__class_id属性。
子类初始化开始......
父类初始化开始......