鱼C论坛

 找回密码
 立即注册
查看: 755|回复: 1

[已解决]python

[复制链接]
发表于 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 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-29 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表