根据类来创建对象称为实例化。
# dog.py
# 在Python中,首字母大写的名称指的是类
class Dog:
"""一次模拟小狗的简单尝试"""
def _init_(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗收到命令时做下"""
print(f"{self.name} is now sitting.")
def roll_over(self):
"""模拟小狗收到命令时打滚"""
print(f"{self.name} rolled over!")
方法_init()_
(1)类中的函数称为方法
(2)当你根据Dog类创建新实例时,Python都会自动运行它。
(3)在这个方法名称中,开头和末尾各有两个下划线,这是一种约定,旨在避免Python默认方法与普通方法发生名称冲突。
(4)在这个方法的定义中,形参self必不可少,而且必须位于其他形参的前面。Python调用这个方法来创建Dog实例时,将自动传入实参self。每个实例相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。
(5)以self为前缀的变量可供类中的所有方法使用,可以通过类的任何实例来访问,称为属性。
class Dog:
"""一次模拟小狗的简单尝试"""
def _init_(self, name, age):
"""初始化属性name和age"""
self.name = name
self.age = age
def sit(self):
"""模拟小狗收到命令时做下"""
print(f"{self.name} is now sitting.")
def roll_over(self):
"""模拟小狗收到命令时打滚"""
print(f"{self.name} rolled over!")
my_dog = Dog('Willie', 6)
# 访问属性——句点表示法
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
# 调用方法
my_dog.sit()
my_dog.roll_over()
# 创建多个实例
your_dog = Dog('Lucy', 3)
print(f"Your dog's name is {your_dog.name}.")
print(f"Your dog is {your_dog.age} years old.")
your_dog.sit()
your_dog.roll_over()
动手试一试
# car.py
class Car:
"""一次模拟汽车的简单尝试"""
def _init_(self, make, model, year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
"""返回整洁的描述性信息"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
my_new_car = Car('audi', 'a4', '2009')
print(my_new_car.get_descriptive_name())
创建实例时,有些属性无需通过形参来定义,可在方法_init_()中为其指定默认值。
# car.py
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 = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车里程的消息"""
print(f"This car has {self.odometer_reading} miles on it.")
my_new_car = Car('audi', 'a4', '2009')
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
1、直接修改属性的值
my_new_car.odometer_reading = 23
my_new_car.read_odometer()
2、通过方法修改属性的值
# car.py
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 = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车里程的消息"""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""
将里程表读书设置为指定的值
禁止价格里程表读数往回调
"""
if mileage >= self.odometer_readig:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
my_new_car = Car('audi', 'a4', '2009')
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
my_new_car.read_odometer()
3、通过方法对属性的值进行递增
# car.py
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 = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车里程的消息"""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""
将里程表读书设置为指定的值
禁止价格里程表读数往回调
"""
if mileage >= self.odometer_readig:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""将里程表读数增加指定的量"""
self.odometer_reading += miles
my_new_car = Car('audi', 'a4', '2009')
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
my_new_car.read_odometer()
my_new_car.increment_odometer(100)
my_new_car.read_odometer()
动手试一试