1 最简单的一个类
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() + " rolled over") my_dog = Dog('willie',)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + "years old.") my_dog.sit()
my_dog.roll_over()
__init__是初始化类
类的属性和方法的 访问方式 默认的self 是自身,函数可以编写这个形参,但是实际不用传self
2 一个类 可以创建多个实例
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() + " rolled over") my_dog = Dog('willie',)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + "years old.") my_dog.sit()
my_dog.roll_over() your_dog = Dog('lucy',)
print("Your dog's name is " + your_dog.name.title() + ".")
print("Your dog is " + str(your_dog.age) + "years old.")
3 类的多个用法
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = def get_descriptive_name(self):
"""返回整洁的描述性信息"""
long_name = str(self.year) + " " + self.make + '' + self.model
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 = mileage def increment_odometer(self,miles):
self.odometer_reading += miles def fill_gas_tank(self):
print("This car doesn't need a gas tank") my_new_car = Car('audi', 'a4' ,'')
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer() my_new_car.odometer_reading=
my_new_car.read_odometer() my_new_car.update_odometer()
my_new_car.read_odometer() my_new_car.increment_odometer()
my_new_car.read_odometer()
可以给类添加默认值,也可以修改类中的属性的值 多重练习
4 类的继承
class ElectricCar(Car):
"""电动汽车的独特之处""" def __init__(self, make, model,year):
"""初始化父类的属性"""
super().__init__(make,model,year)
self.battery_size = def describe_battery(self):
print("This car has a " + str(self.battery_size) +"-kwh battery.") def fill_gas_tank(self):
print("This is OK!") my_tesla = ElectricCar('tesla','model s',)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
my_tesla.fill_gas_tank()
紧跟着上面,创建一个子类,可以在子类中复写父类的方法
子类和父类使用关键字super()关联 父类也叫做超类,这是super的由来
5 把类集中放到 其他的类中,也可以方便的调用
class Battery():
def __init__(self,battery_size=):
"""初始化电瓶的属性"""
self.battery_size = battery_size def describe_battery(self):
"""打印一天描述电瓶容量的消息"""
print("This car has a " +str(self.battery_size) + "-kwh battery.") def get_range(self):
if self.battery_size == :
range =
elif self.battery_size == :
range = message = "This car can go approximately " + str(range)
message +=" miles on a full charge"
print(message) class ElectricCar(Car):
"""电动汽车的独特之处""" def __init__(self, make, model,year):
"""初始化父类的属性"""
super().__init__(make,model,year)
self.battery = Battery() my_tesla = ElectricCar('tesla','model s',)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()