如何理解python类中列表的属性

2025-03-17 04:12:33
推荐回答(1个)
回答1:

就比方说 有一个类 叫做 car

这个类的属性 可以有 color size brand price year 等描述性的东西

这个类的方法 可以是 run stop forward backward 等执行性的东西

class car:  
#定义基本属性  
    color = ''  
    size = 0  
    brand = ''
    price = 0 
    year = 0 
 
#定义构造方法  
    def __init__(self):  
        self.color = color
        self.size = size
        self.brand = brand
        self.price = price
        self.year = year
    def run(self):  
        print("the car is running" )
    def stop(self):  
        print("the car is stop" )
    def forward(self):  
        print("the car is forward" )
    def backward(self):  
        print("the car is backward" )
   
#类调用
 
benz = car('red',1.8T,'Mercedes',400000,2016)  
benz.run()  
benz.stop() 
benz.forward() 
benz.backward()