1 封装
1.1 定义类
- 语法
class Person():
..code..
- 示例
# 定义一个类
class Person():
def name(self):
print("我叫Xhh")
# 创建对象
xhh = Person()
# 使用对象
xhh.name()
1.2 self
self值的是调用该函数的对象
1.3 添加和获取对象属性
1.3.1 类外添加对象属性
# 创建对象
xhh = Person()
# 类外添加属性
xhh.age = 18
xhh.height = 180
# 获取属性
print(f'XHH的年龄是{xhh.age}')
1.3.2 类内获取对象属性
- 语法
self.属性名
1.4 xx( ) 魔法方法
1.4.1 init( ) 构造
初始化对象,即构造函数
class Person():
# __init__ 初始化对象(默认构造函数)
def __init__(self):
self.name = 'xhh'
self.age = 18
def myInfo(self):
print(f'姓名:{self.name}')
print(f'年龄:{self.age}')
1.4.2 init( ) 带参构造
带参构造
class Person():
# __init__ 初始化对象(构造函数)
def __init__(self, name, age):
self.name = name
self.age = age
def myInfo(self):
print(f'姓名:{self.name}')
print(f'年龄:{self.age}')
p2 = Person('zph', 19)
p2.myInfo()
1.4.3 str( )
print输出对象默认为对象的地址。
定义str后,就打印该方法下的 return 内容
class Person():
def __init__(self):
self.name = 'xhh'
def __str__(self):
return "这里是打印对象print(对象)的内容"
p1 = Person()
print(p1) # 这里是打印对象print(对象)的内容
1.4.4 del( ) 析构函数
析构函数
def __del__(self):
print("对象已删除")
### 1.4.5 烤地瓜
class SweetPotato():
# 初始化属性
def __init__(self):
self.cookTime = 0
self.cookState = '生的'
self.condiments = []
def cook(self, time):
"""烤地瓜的方法"""
self.cookTime += time
if 0 <= self.cookTime < 3:
self.cookState = '生的'
elif 3 <= self.cookTime <5:
self.cookState = '半生不熟'
else:
self.cookState = '熟了'
def addCondiments(self, condiment):
self.condiments.append(condiment)
def __str__(self):
return f'地瓜烤了{self.cookTime}分钟,状态是{self.cookState}, 调料{self.condiments}'
digua1 = SweetPotato()
digua1.cook(4)
digua1.addCondiments('辣椒')
print(digua1)
2 继承
- 单继承、多继承
- 子类重写父类的同名属性和方法
- 子类调用父类的同名属性和方法
- 多层继承
- super( )
- 私有属性和方法
2.1 单/多继承的写法
多继承,默认继承第一个父类同名方法
# 父类School1 默认继承object
class School1(object):
def __init__(self):
self.name = 'njust'
def func(self):
print("school1的func")
# 父类School2 默认继承object
class School2(object):
def __init__(self):
self.name = 'xhh'
def func(self):
print("school2的func")
# 子类 继承 多继承
class Son(School1, School2):
pass
son1 = Son()
print(son1.name) # njust
print(son1.func()) # school1的func
2.2 子类重写父类的同名属性和方法
重写后,调用的是子类的同名方法和属性
# 父类School1 默认继承object
class School1(object):
def __init__(self):
self.name = 'njust'
def func(self):
print("school1的func")
# 子类 继承
class Son(School1):
def __init__(self):
self.name = "son 's njust"
def func(self):
print("son 's school1的func")
son1 = Son()
print(son1.name) # son 's njust
print(son1.func()) # son 's school1的func
2.3 拓展 _mro顺序
# 打印父类,及继承关系, 类名.__mro__
print(Son.__mro__)
2.4 子类调用父类的同名方法和属性
# 父类School1 默认继承object
class School1(object):
def __init__(self):
self.name = 'njust'
def func(self):
print("school1的func")
# 子类 继承 多继承
class Son(School1):
def __init__(self):
self.name = "son 's njust"
def func(self):
print("son 's school1的func")
# 子类调用父类同名属性和方法:在次封装,并进行父类初始化
def funcSchool1(self):
School1.__init__(self)
School1.func(self)
son = Son()
son.funcSchool1() # school1的func
2.5 super( ) 调用父类的方法
# 父类School1 默认继承object
class School1(object):
def __init__(self):
self.name = 'njust'
def func(self):
print("school1的func")
# 子类 继承
class Son(School1):
def __init__(self):
self.name = "son 's njust"
def func(self):
print("son 's school1的func")
def func_School(self):
# # 子类调用父类同名属性和方法:利用 super()方法实现
# # 1.方法一:带参 super(当前类名, self).函数()
# super(Son, self).__init__()
# super(Son, self).func()
# 2.方法二:无参 super().函数()
super().__init__()
super().func()
son = Son()
son.func_School() # school1的func
2.6 __私有权限
私有属性,无法继承给子类
class School1(object):
def __init__(self):
self.__money = 6666 # 私有属性
def __sFunc(self): # 私有方法
pass
2.7 获取和修改私有属性
类内获取和修改,函数名 getxx( ) & setxx( )
class Son(object):
def __init__(self):
self.__money = 6666 # 私有属性
def setMoney(self, val):
self.__money = val
def getMoney(self):
return self.__money
son = Son()
print(son.getMoney())
son.setMoney(15)
print(son.getMoney())
3 多态
3.1 了解多态
3.2 体验多态
class Dog(object):
def work(self):
print("指哪打哪!!!") # 父类只提供方法,pass 空也是可以的
class ArmyDog(Dog): # 继承Dog类
def work(self): # 子类重写父类同名方法
print("我是ArmyDog~")
class DrugDog(Dog):
def work(self):
print("我是DrugDog!")
class Person(object):
def workWithDog(self, dog): # 传入不同对象,执行不同的代码->使用更广
dog.work()
# 创建对象
armyDog = ArmyDog()
drugDog = DrugDog()
xhh = Person()
xhh.workWithDog(armyDog) # 我是ArmyDog~
xhh.workWithDog(drugDog) # 我是DrugDog~
3.3 类属性
3.3.1 创建类属性
class Person(object):
name = 'xhh'
age = 18
p1 = Person()
p1.name
3.3.2 修改类属性
class Person(object):
name = 'xhh'
age = 18
# # 1.通过类修改类属性(使用本法)
# Person.age = 66
# print(Person.age) # 66
# 2.通过实例修改类属性(仅仅修改本对象的属性)
p1 = Person()
p1.age = 22
print(p1.age) # 22
print(Person.age) # 18
3.4 类方法和静态方法
3.4.1 类方法
class Person(object):
__name = 'xhh' # 类(__私有)属性
@classmethod # 类方法
def getName(cls):
return cls.__name
p1 = Person()
print(p1.getName())
3.4.2 静态方法
class Person(object):
@staticmethod
def myInfo():
print("静态方法,不需要任何实例化对象/类")
p1 = Person()
# 调用的两种方式
p1.myInfo()
Person.myInfo()
4 异常
有一句代码可能错误,也可能不错误,就放在异常中书写