创建类
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
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',6)
your_dog = Dog('lucy',3)
# print("My dog's name is " + my_dog.name.title() + ".")
# print("My dog is " + str(my_dog.age) + " years old.")
my_dog.sit()
print(" Your dog's name is " + your_dog.name.title() + ".")
print("Your dog is " + str(your_dog.age) + " years old.")
your_dog.sit()
给属性指定默认值
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
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.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""打印一条指出汽车里程的消息"""
print("This car has " + str(self.odometer_reading) + " miles on it.")
my_new_car = Car('audi','a3',2016)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
修改属性的值:
1:直接修改属性的值
#!/usr/bin/env python # --*-- encoding:utf-8 --*-- 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.make + ' ' + self.model return long_name.title() def read_odometer(self): """打印一条指出汽车里程的消息""" print("This car has " + str(self.odometer_reading) + " miles on it.") my_new_car = Car('audi','a3',2016) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer()
2:通过方法修改属性的值
#!/usr/bin/env python # --*-- encoding:utf-8 --*-- 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.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 my_new_car = Car('audi','a3',2016) print(my_new_car.get_descriptive_name()) my_new_car.update_odometer(23) my_new_car.read_odometer()
#!/usr/bin/env python # --*-- encoding:utf-8 --*-- class Car(): def __init__(self,make,model,year): """模拟汽车的的属性""" self.make = make self.model = model self.year = year self.odometer_reading = 25 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): """将里程表读数设置为指定的值 禁止将里程表读数往回调""" if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") my_new_car = Car('audi','a3',2016) print(my_new_car.get_descriptive_name()) my_new_car.update_odometer(23) my_new_car.read_odometer() 执行结果: 2016 Audi A3 You can't roll back an odometer! This car has 25 miles on it.
3:通过方法对属性的值进行递增
继承
如果编写的类是另一个现成类的特殊版本,可使用继承。一个类集成另一个类时,它将自动获得另一个类的所有属性和方法;原有的类称为父类,而新类称为子类。
子类继承了其父类的所有属性和方法,同时还可以定义自己的属性和方法。