首页 > 代码库 > class.py

class.py

class Dog():                            #创建类    """一次模拟小狗的简单尝试"""    def __init__(self,name,age):        """初始化属性name和age"""        self.name = name        self.age = age    def sit(self):        """模拟小狗被命令时蹲下"""        print(self.name.title()+ "is now sitting.")    def roll_over(self):        """模拟小狗被命令时打滚"""        print(self.name.title()+"roll over!")obj=Dog(whille, 6)                #根据类创建实例print(obj.name)obj.sit()obj.roll_over()

class Car():                                    #Car类    """一次模拟汽车的简单尝试"""    def __init__(self,make,model,year,):        """初始化描述汽车的属性"""        self.make  = make        self.model = model        self.year = year        self.odometer_reading = 0         #给属性指定默认值    def get_descriptive_name(self):        """返回整洁的信息"""        long_name = str(self.year)+ +self.make +  +self.model        print(long_name)        return long_name.title()    def read_odometer(self):        """打印一条指出汽车里程的信息"""        print("This car has " + str(self.odometer_reading) + " miles on it ")    def update_odometer(self, mileage):         #用方法修改属性的值        self.odometer_reading = mileagenew_car = Car(audi,a4,2016)new_car.get_descriptive_name()new_car.odometer_reading = 23          #直接修改属性默认值new_car.read_odometer()

 

 

class.py