函数基本概念
-
在程序中分离不同任务
-
代理模式
函数分类
- 内置函数
- abs()
- len()
- 标准库函数
- math, random(通过import语句导入)
- 第三方库函数
- Python图像库
- 用户自定义函数
函数的定义和调用
- 使用def语句定义函数
def 函数名(参数列表):
函数体 | 语句
- 函数名:
- 全部为小写字母
- 可以使用下划线增加可阅读性:my_show
- 参数与参数之间用逗号作为间隔
计算并返回第n阶调和数: 1+1/2+1/3+1/4+…+1/n
def f(n):
i=1
sum=0.0
while(i<=n):
sum+=1.0/i;
i=i+1;
return sum;
result = f(3)
print(result)
类的定义
类是一个数据结构
class 类名:
类体
- 类名要大写
class Student:
pass
对象的创建和调用
对象名 = 类名(参数列表)
对象.属性
对象.方法
class Student:
pass
p = Student()
print(p)
<__main__.Student object at 0x000001E3F83FEEF0>
>>> class Student:
def __init__(self,name,age):
self.name = name
self.age = age
def say_hi(self):
print('hi',self.name)
>>> p = Student('haha',19)
>>> p.name
'haha'
>>> p.say_hi()
hi haha
封装
class Car:
count = 0
name = "aodi"
c = Car()
print(c.name)
print(Car.name)
Car.count += 1
print(Car.count)
print(c.count)
#
aodi
aodi
1
1
封装属性:类名.方法调用私有属性
- 在属性前面加上两个下划线
class Person:
name = 'tom'
__age = 19
def get_info():
print(Person.__age)
p = Person()
print(Person.name)
Person.get_info()
#
tom
19
调用私有属性
class Person:
name = 'tom'
__age = 19 #相当于Java:private static int age = 19;
def get_info():
print(Person.__age)
p = Person()
p._Person__age # 对象._类名__私有属性
#
19
class Person:
def __init__(self,name):
self.__name = name # 相当于Java:private String name;
p = Person('tom')
p._Person__name
#
'tom'
python装饰器:java@overload
@property装饰器
- 属性装饰器的作用类似于get方法
class Person:
def __init__(self,name):
self.__name = name
@property
def info(self):
return self.__name
p = Person('tom')
print(p.info) # 把该方法当成一个属性来出现
#
tom
name.setter name.getter
class Person:
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter #name属性的setter方法
def name(self,value):
self.__name = value
@name.getter
def name(self):
return self.__name
@name.deleter
def name(self):
del self.__name
p = Person('tom')
p.name = 'allen'
print(p.name)
# del p.__name # 私有属性不可以进行删除操作
#输出: allen
property(getter,setter,deleter,doc):映射关系
class Person:
def __init__(self,name):
self.__name = name
def getName(self):
return self.__name
def setName(self,value):
self.__name = value
def delName(self):
del self.__name
# property(getter,setter,deleter,doc):映射关系
name = property(getName,setName,delName,'我是name属性')
p = Person('tom')
print(p.name)
p.name = 'haha'
print(p.name)
'''输出
tom
haha
'''
自定义属性
# 为类新增加一个属性
class C1:
pass
o = C1()
o.name = '自定义' # 在o这个对象中创建了一个name属性
print(o.name)
'''输出
自定义
'''
用字典的方式实现
class C1:
pass
o = C1()
o.name = '自定义' # 在o这个对象中创建了一个name属性,name属性是公有的
print(o.__dict__)
'''输出
{'name': '自定义'}
'''
getattr getattribute setattr delattr
class Person:
def __init__(self):
pass
def __getattribute__(self,name):
return str.upper(object.__getattribute__(self,name))
def __setattr__(self,name,value):
object.__setattr__(self,name,str.strip(value))
o = Person()
o.firstname = 'china'
print(o.firstname)
'''
CHINA
'''
方法
实例方法
- 第一个参数必须是self
- 对象传参的时候不需要为self进行传参
def 方法名称(self,[参数列表])
class Person:
def say_hi(self,name):
self.name = name
print('hi',self.name)
p = Person()
p.say_hi('鹅鹅鹅')
'''
hi 鹅鹅鹅
'''
静态方法
- 不能对特定的实例进行操作
- 参数列表里面不需要写self
- 用类名来调用静态方法,或者用对象来调用
@staticmethod
def 方法名([参数列表])
class Person:
@staticmethod
def info(c):
return c
print(Person.info('wuwu'))
p = Person()
print(p.info('haha'))
'''
wuwu
haha
'''
类方法
- 不需要为cls传参
- 用类名调用
@classmethod
def 方法名(cls,[参数列表]):
class Foo:
classname = 'Foo'
def __init__(self,name): #构造器
self.name = name
def f1(self): #实例方法
print(self.name)
@staticmethod
def f2():
print('static')
@classmethod
def f3(cls):
print(cls.classname)
f = Foo('中')
f.f1()
Foo.f2()
Foo.f3()
'''
中
static
Foo
'''
私有方法
- 以两个下划线开头,不以两个下划线结尾
class Methods:
def publicMethod(self):
print('公有方法')
def __privateMethod(self):
print('私有方法')
def publicMethod2(self):
self.__privateMethod()
m = Methods()
m.publicMethod() # 对象调用self不需要传参
m._Methods__privateMethod()
m.publicMethod2()
print('--------')
Methods.publicMethod(m) #类调用self必须传参,且传的参数必须是一个对象
Methods._Methods__privateMethod(m)
'''
公有方法
私有方法
私有方法
--------
公有方法
私有方法
'''
方法重载
- 方法名称相同,但参数列表不同
class Person:
def say_hi(self,name):
self.name = name
print('hi',self.name)
def say_hi(self,name,age):
print('你好,我叫{0},年龄是{1}'.format(name,age))
p = Person()
p.say_hi('鹅鹅鹅',20)
p.say_hi('鹅鹅鹅') #报错
'''
你好,我叫鹅鹅鹅,年龄是20
'''
继承
- Python是多继承
- 子类:派生类 父类:基类
class 子类(父类1,父类2,...):