class DataBaseInterface: def __init__(self, db_name, db_path, tables): self.db_name = db_name self.db_path = db_path self.tables = tables def Init_Database(self): pass def Search_Database(self, column, conditions): pass def Update_Database(self, update_type, table, column): pass def Uninit_Database(self): pass class ExcelDB(DataBaseInterface): def __init__(self, db_name, db_path, tables): super().__init__(db_name, db_path, tables) self.sheets = [] self.active_sheet = "Sheet1" def Init_Database(self): print("ExcelDB Init_Database") def Search_Database(self, column, conditions): print("ExcelDB Search_Database") def Update_Database(self, update_type, table, column): print("ExcelDB Update_Database") def Uninit_Database(self): print("ExcelDB Uninit_Database") db = ExcelDB("BCSC_STAFFS", ".", ["WX", "SZ", "SH"]) db.Init_Database() db.Search_Database("Name", "Startwith A") db.Update_Database("Add", "WX", "Name") db.Uninit_Database() class Sqlite(DataBaseInterface): def __init__(self, db_name, db_path, tables): super().__init__(db_name, db_path, tables) self.indexs = [] def Init_Database(self): print("Sqlite Init_Database") def Search_Database(self, column, conditions): print("Sqlite Search_Database") def Update_Database(self, update_type, table, column): print("Sqlite Update_Database") def Uninit_Database(self): print("Sqlite Uninit_Database") db = Sqlite("BCSC_STAFFS", ".", ["WX", "SZ", "SH"]) db.Init_Database() db.Search_Database("Name", "Startwith A") db.Update_Database("Add", "WX", "Name") db.Uninit_Database() class Oracle(DataBaseInterface): def __init__(self, db_name, db_path, tables): super().__init__(db_name, db_path, tables) self.indexs = [] self.relations = [] def Init_Database(self): print("Oracle Init_Database") def Search_Database(self, column, conditions): print("Oracle Search_Database") def Update_Database(self, update_type, table, column): print("Oracle Update_Database") def Uninit_Database(self): print("Oracle Uninit_Database") db = Oracle("BCSC_STAFFS", ".", ["WX", "SZ", "SH"]) db.Init_Database() db.Search_Database("Name", "Startwith A") db.Update_Database("Add", "WX", "Name") db.Uninit_Database() class Animal: def __init__(self, name, gender, age): self.gender = gender self.age = age self.name = name def __enter__(self): print("Animal __enter__") return self def speak(self): print("I am " + self.name) def __exit__(self, exc_type, exc_val, exc_tb): print("Animal __exit__") print("type:", end="") print(exc_type) print("val:", end="") print(exc_val) print("trace:", end="") print(exc_tb) # with Animal("xxxxx", "male", 3) as an: # an.speak() # # exit() class Dog(Animal): def __init__(self, name, gender, age, color, speed): super().__init__(name, gender, age) self.color = color self.speed = speed def run(self): print("Dog Speed is ", self.speed) def speak(self): print("This is dog: ", self.name) class Cat(Animal): def __init__(self, name, gender, age, color, speed): super().__init__(name, gender, age) self.color = color self.speed = speed def run(self): print("Cat Speed is ", self.speed) def speak(self): print("This is cat: ", self.name) animals = [] mm = Cat("miao", "female", 8, "white", 25) animals.append(mm) ww = Dog("wang", "male", 10, "black", 20) animals.append(ww) for an in animals: an.run() an.speak()
Basic Python Learning--Polymorphism
最新推荐文章于 2024-08-27 16:38:10 发布