开始使用免费开始使用

切换编程范式

有时会出现这种情况:您以某种范式开始一个项目,后来才发现这并不太适合当前任务。您现在有一个用于存储汽车销售信息的简单过程式程序,需要将其转换为面向对象的程序,以便更容易扩展。

本练习是课程的一部分

编程范式概念

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Old procedural code
def buy_car(budget, price):
    if budget > price:
        print("You can buy this car!")
    else:
        print("This car is too expensive for now.")

# Define the Car class
____ Car():
    # Initialize each car with a price
    def __init__(____, price):
        self.price = ____
编辑并运行代码