创建简单的类和实例
在python中,首字母大写的名称指的是类,这个类定义中括号的内容是空的。
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。
class Dog(): #首字母大写的名称代表的是类,类定义中的括号是空的 """一次模拟小狗的简单尝试""" def __init__(self, name, age): #_init_()是一个特殊的方法,每次创建新实例python都会运行它 """初始化属性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() + " rolled over!") my_dog = Dog("zs", 4) #定义两只小狗 your_dog = Dog("ls", 5) print("my dog's name " + my_dog.name.title() + "!") print("your dog's name " + your_dog.name.title() + "!") my_dog.sit() your_dog.roll_over()
运行结果为:
my dog's name Zs! your dog's name Ls! Zs is now sitting. Ls rolled over!
使用类和实例
class 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.model + ' ' + self.make return long_name.title() #此处必须有返回值 def read_odometer(self): """打印一条汽车里程的消息""" print("this car has " + str(self.odometer_reading) + " miles on it.") new_car = Car('audi', 'a4', 3) print(new_car.get_descriptive_name()) new_car.read_odometer()
运行结果为:
3 A4 Audi
this car has 0 miles on it.
修改属性的值
可以用三种方法修改属性的值;
(1)直接通过实例进行修改;
(2)通过方法进行设置;
(3)通过方法进行递增;
class 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.model + ' ' + self.make return long_name.title() #此处必须有返回值 def read_odometer(self): """打印一条汽车里程的消息""" print("this car has " + str(self.odometer_reading) + " miles on it.") new_car = Car('audi', 'a4', 3) new_car.read_odometer() new_car.odometer_reading = 3 #直接修改属性的值 new_car.read_odometer()
class 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.model + ' ' + self.make return long_name.title() #此处必须有返回值 def update_odometer_reading(self, meter): #此处添加一个方法,这个方法可以接受里程值,并进行存储。 self.odometer_reading = meter def read_odometer(self): """打印一条汽车里程的消息""" print("this car has " + str(self.odometer_reading) + " miles on it.") new_car = Car('audi', 'a4', 3) new_car.update_odometer_reading(5) #调用方法,并传递实参5 new_car.read_odometer()
有时候需要将属性值递增特定的量,而不是将其设置为一个全新的值。
class 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.model + ' ' + self.make return long_name.title() #此处必须有返回值 def incream_odometer_reading(self, meter): #此处添加一个方法,这个方法可以接受里程值,并将其加入到原有值。 self.odometer_reading += meter def read_odometer(self): """打印一条汽车里程的消息""" print("this car has " + str(self.odometer_reading) + " miles on it.") new_car = Car('audi', 'a4', 3) new_car.incream_odometer_reading(5) #调用方法,并传递实参5 new_car.read_odometer() new_car.incream_odometer_reading(5) #调用方法,再次传递实参5 new_car.read_odometer()
继承
一个类继承另一个类时,他将自动获得另一个类的所有属性和方法;原有的类称为父类,新的类称为子类;
class 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.model + ' ' + self.make return long_name.title() #此处必须有返回值 def read_odometer(self): """打印一条汽车里程的消息""" print("this car has " + str(self.odometer_reading) + " miles on it.") class ELE(Car): #定义子类时必须在括号内指定父类的名称 """d电动车的独特之处""" def __init__(self, make, model,year): """初始化父类的属性""" super().__init__(make, model, year) #super()是一个特殊函数,将父类和子类关联起来 my_ele = ELE('aa', 'ss',4) my_ele.read_odometer() print(my_ele.get_descriptive_name())