|
发表于 2024-11-25 14:22:16
|
显示全部楼层
本楼为最佳答案
[code=python]
import random
class BankAccount:
def __init__(self, balance=0.0):
self.account_number = self.generate_account_number()
self.balance = balance
def generate_account_number(self):
return str(random.randint(10000000, 99999999))
def deposit(self, amount):
self.balance += amount
print(f"Deposited {amount}. New balance is {self.balance}.")
def withdraw(self, amount):
if amount > self.balance:
print("Withdrawal failed: Not enough balance.")
else:
self.balance -= amount
print(f"Withdrew {amount}. New balance is {self.balance}.")
def get_balance(self):
return self.balance
class Appliance:
def __init__(self, brand, model, power):
self.brand = brand
self.model = model
self.power = power
def get_info(self):
return f"Appliance: {self.brand} {self.model}, Power: {self.power}W"
def turn_on(self):
pass
def turn_off(self):
pass
class Refrigerator(Appliance):
def turn_on(self):
print(f"{self.brand} {self.model} refrigerator is now ON.")
def turn_off(self):
print(f"{self.brand} {self.model} refrigerator is now OFF.")
class WashingMachine(Appliance):
def turn_on(self):
print(f"{self.brand} {self.model} washing machine is now ON.")
def turn_off(self):
print(f"{self.brand} {self.model} washing machine is now OFF.")
# 示例使用:
bank_account = BankAccount()
bank_account.deposit(100)
bank_account.withdraw(50)
print(bank_account.get_balance())
fridge = Refrigerator("LG", "GR-349SQF", 200)
print(fridge.get_info())
fridge.turn_on()
fridge.turn_off()
washer = WashingMachine("Samsung", "WF42H5200AW", 500)
print(washer.get_info())
washer.turn_on()
washer.turn_off()
[/code]
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|