Python类学习笔记
面向对象思想
面向对象:将数据和函数绑定到一起,进行封装,提高开发程序的速度,减少代码的书写
类和对象是面向对象里面两个很重要的概念,面向对象是软件开发的编程思想,或者说是一种开发方式
对象是类的实例化,类是创建对象的模板
类:具有相同特征和相同功能一组对象的集合
生活中常见的类:动物类,植物类,人类
以人类进行举例
相同特征(成员属性):身高 体重 性别 年龄
相同功能(成员方法):吃饭 睡觉 喝水
对象:具体的某个事物
一台笔记本,一个水杯
对象和类
类和对象之间的关系:
就像利用玩具模型来创建多种不同的玩具
类就是创建对象的模板
区分类和对象:
奔驰汽车 类
奔驰smart 类
张三开的那辆奔驰smart 对象
水果 类
香蕉 类
李四吃了一半的那个苹果 对象
狗 类
大黄狗 类
王五正在牵着的那只大黄狗 对象
创建类
class Restaurant():
def __init__(self,restaurant_name,*cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print("The restaurant name is %s"%self.restaurant_name)
print('The cuisine types are:')
for type in self.cuisine_type:
print(type)
def open_restaurant(self):
print("The restaurant is open!")
- 方法:类中的函数称为方法
-
方法__init__()是一个特殊的方法,每当根据Restaurant类创建新实
例时,Python都会自动运行它。 -
方法__init__()定义成了包含三个形参:self、restaurant_name和*cuisine_type。在这个方法的定义中,形参self必不可少,还必须位于其他形参的前面。
-
每当我们根据Dog类创建实例时,都只需给最后两个形参(name和age)提供值。
- 属性
- self.name = name获取存储在形参name中的值,并将其存储到变量name中,然后该变量被关联到当前创建的实例。像这样可通过实例访问的变量称为属性。
- 其他两个方法
- Dog类还定义了另外两个方法:describe_restaurant()和open_restaurant()。由于这些方法不需要额外的信息,如名字或年龄,因此它们只有一个形参self。
根据类创建对象
- 创建对象的格式:对象名 = 类名()
my_restaurant=Restaurant('CKC','Kochen','Nudeln')
- 访问属性
print(my_restaurant.restaurant_name) #句点法
结果:
CKC
- 调用方法
my_restaurant.describe_restaurant()
结果:
The restaurant name is CKC
The cuisine types are:
Kochen
Nudeln
my_restaurant.open_restaurant()
结果:
The restaurant is open!
使用类和实例
- 给属性设置默认值
class Restaurant():
def __init__(self,restaurant_name,*cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
self.number_served=0 #设置默认
def describe_restaurant(self):
print("The restaurant name is %s"%self.restaurant_name)
在方法__init__()内指定这种初始值是可行的;如果你对某个属性这样做了,就无需包含
为它提供初始值的形参。
- 修改属性
#句点法修改属性:
restaurant.number_served=20
restaurant.people_ate_number()
输出:The number of people ate in this restaurant is:20
#通过方法修改
def set_number_served(self,number):
self.number_served=number
restaurant.set_number_served(29)
#通过方法对属性的值进行递增
def increment_number_served(self,i):
self.number_served+=i
restaurant. increment_number_served(20)
继承
class Restaurant():
--snip--
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,flavors,*cuisine_type):
super().__init__(restaurant_name,*cuisine_type)
self.flavors=flavors
def favorite_icecream(self):
for i in self.flavors:
print(i)
- 创建子类时,父类必须包含在当前文件中,且位于子类前面。
- 的super()是一个特殊函数,帮助Python将父类和子类关联起来。这行代码让Python调用Restaurant的父类的方法__init__(),让IceCreamStand实例包含父类的所有属性。父类也称为超类(superclass),名称super因此而得名。
- 在子类中添加一个了名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表。
#调用
my_icecream=IceCreamStand('Ckc',['香草','巧克力','草莓'],'Kochen','Kattofeln')
my_icecream.favorite_icecream()
结果:
香草
巧克力
草莓
-
重写父类的方法
可在子类中定义一个方法,它与要重写的父类方法同名 -
将实例作为属性
class Restaurant():
--snip--
class Obst():
--snip--
def getobst(self):
--snip--
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,*cuisine_type):
super().__init__(restaurant_name,*cuisine_type)
self.obst=Obst()
#调用
my_retaurant=IceCreamStand(--snip--)
my_retaurant.Obst.getobst()
定义一个名为Obst的新类,并在IceCreamStand类中添加一个self.obst的属性,
这行代码让Python创建一个新的Obst实例,并将该实例存储在属性self.obst中
导入类
将Restaurant类存储在一个文件retaurant中。在另一个文件中,导入Restaurant类
语法:from restaurant import Restaurant
ihr_restaurant=Restaurant('Huijiayi','love','betray')
ihr_restaurant.describe_restaurant()
- 也可以导入多个类
from restaurant import Restaurant,IceCreamStand
#类用逗号分隔
- 导入整个模块
import Restaurant
#语法module_name.class_name访问需要的类。
my_restaurant=restaurant.Restaurant('')
#调用
my_restaurant.describe_restaurant()
- 导入所有类
from module_name import *
#不推荐
1.这种导入方式没有明确地指出你使用了模块中的哪些类
2.不小心导入了一个与程序文件中其他东西同名的类,将引发难以诊断的错误。
Tips:需要从一个模块中导入很多类时,最好导入整个模块,并使用module_name.class_name语法来访问类。这样做时,虽然文件开头并没有列出用到的所有类,但你清楚地知道在程序的哪些地方使用了导入的模块;你还避免了导入模块中的每个类可能引发的名称冲突。
- 在一个模块中导入另一个模块
将Restaurant存在a文件中,将IceCreamStand存在b中
在IceCreamStand中先from a import Restaurant,然后再继承
在文件c中,from a import Restaurant,from b import IceCreamStand,
即可创建任何类型的restaurant