前端学习python第四天 ,面向对象
·
#基础
class Car:
#__init__方法的初始化的方法,会在对象创建的自动调用,可以在该方法中为对象设置对应的属性
#self:是第一参数。表示当前所创建出来的实列对象
def __init__(self, color, brand, name,price):
self.color = color
self.brand = brand
self.name = name
self.price = price
print("类型对象初始化完成")
#创建实列
c1 = Car("红色", "bmw", "xy", 8000)
print("@",c1.__dict__)
c2 = Car("LV色", "bmw", "xy", 8000)
print("@",c2.__dict__)
#基础
class Car:
#__init__方法的初始化的方法,会在对象创建的自动调用,可以在该方法中为对象设置对应的属性
#self:是第一参数。表示当前所创建出来的实列对象
def __init__(self, color, brand, name,price):
self.color = color
self.brand = brand
self.name = name
self.price = price
print("类型对象初始化完成")
#定义是实列方法
def runing(self):
print(f"{self.brand}{self.name}{self.price}")
def total_cost(self, discount,rate):
total_cost = self.price * discount+rate*self.price
return total_cost
#创建实列
c1 = Car("红色", "bmw", "xy", 8000)
#调用实列方法
c1.runing()
total =c1.total_cost(0.9,0.1)
print("@@",total)
更多推荐


所有评论(0)