class Smartphone: def_init_(self< brand, model, storage, camera_resolution): self.brand=brand self.model=model self.storage=storage self.camera_resolution=camera_resolution self.is_on= False self.apps=[]
def power_on(self): self.is_on= True print(f"{self.brand} {self.model} is powering on...")
def power_off(self): self.is_on= False print(f"{self.brand} {self.model} is powering off...") def install_app(self, app_name): if self.is_on: self.apps.append(app_name) print(f"installing{app_name}...") else: print("Please power on the smartphone first.") def unstall_app(self, app_name): if self.is_on and app_name in self.apps: self.apps.remove(app_name)
print(f"Uninstalling{app_name}...") else: print(f"{app_name} is not installed or the phone is off.")
def take_photo(self): if self.is_on: print(f"Taking a photo with{self.camera_resolution} camera...") else: print("Please power on the smartphone first.") def get_specs(self): return f"Brand: {self.brand}, Model: {self.model}, Storage: {self.storage}, Camera: {self.camera_resolution}" lass GamingSmartphone(Smartphone): def_init_(self, brand,model, storage, camera_resolution,cooling_system): super().init(brand, model, storage camera_resolution) sel.cooling_system=cooling_system def start_game(self, game name): f self.is_on: print(f"Starting {game_name} with {self.cooling_system} cooling...") else: print("Please power on the smartphone first.") def get_specs(self): parent_specs=super().get_specs() return f"{parent_specs}, cooling:{self.coling_system}"
my_phone=Smartphone("HMD", "mkopa x20", "256GB", "50MP") my_phone.power_on() my_phone.install_app(facebook") my_phone.take_photo() print(my_phone.get_specs()) my_phone.power_off()
gaming_phone=GamingSmartphone("ROG","Pnone 9", "532GB" 74MP", "Vapor Chamber") gaming_phone.power_on() gaming_phone.install_app("Genshin Impact") gaming_phone.start_game("Genshin Impact") print(gaming_phone.power_off() #Activity 2: polymorphism challenge! class Animal: def_init_(self,name): self.name=name def move(self): print("generic animal movement") #Default implementation class Car: def move(self): print("driving")
class Plane: def move(self): print("Flying")
class Fish(Animal): def move(self): print("Swimming") #Polymorphism in action animals=[Car(), Plane(), Dog("Joginda"), Fish("Mbuta")] for animal in animals: animal.move()