书接上回:
1. 类的定义
(1)class 类名[(父类)]:
pass(类体不写时直接写pass也可以)
(2)类名每个首字母大写;父类不写默认继承object类
2. 类的成员
(1)类外的叫函数
(2)类内定义的变量叫类属性,被该类的所有对象共享,即所有对象该属性保持一致,可以直接用类名调用
(3)类内前面用@staticmethod修饰的是静态方法,不能加上self,直接类名调用
(4)类内前面用@classmethod修饰的是类方法,必须加上cls,直接类名调用
(5)类内前面没有任何修饰的是实例方法,必须加上self
(6)__init__(self)是类的初始化方法
3. 对象的创建
(1)对象的创建又称之为实例化
(2)语法:实例名=类名([__init__()方法传参])
例3:
def over_class_func1(): # 函数
print('over_class_func1')
class Test: # 类名:
age = 1 # 类属性
def __init__(self, name): # 初始化方法
self.name = name # 等号左边:实例属性;等号右边:局部变量
def instance_func1(self): # 实例方法
print('instance_func1: my name is ', self.name)
@staticmethod
def satic_func1(): # 静态方法
print('static_func1')
@classmethod
def class_func1(cls): # 类方法
print('class_func1')
# ----------------------1. 类属性------------------------------
print(Test.age) # 调用类属性
test1 = Test('sunny') # 创建对象
test2 = Test('rainy') # 创建对象
print(test1.age)
print(test2.age)
Test.age = 18
print(test1.age) # 18
print(test2.age) # 18
# ---------------------2.类方法----------------------------
Test.class_func1()
# ---------------------3.静态方法---------------------------
Test.static_func1()
# ---------------------4.实例方法---------------------------
test1.instance_func1()
test2.instance_func1()
# ---------------------5.动态绑定属性----------------------------
test2.home = '天津' # 为test2这个实例对象添加一个属性
print(test1.name, test1.age)
print(test2.name, test2.age, test2.home)
# print(test1.home) # 会报错
# ---------------------6.动态绑定方法-----------------------------
def show():
print('show')
test1.show = show # 为test1这个实例对象添加一个方法
test1.show()
# test2.show() # 会报错
4. 类的封装
(1)封装:提高程序的安全性
(2)将属性封装到方法中,在类外不可以调用该属性,也即不可以对该属性进行随意的更改
(3)在Python中,若不想属性被外部访问,可在属性名前面加两个下划线__
(4)私有属性的访问方式一:可以通过方法访问私有属性
(5)私有属性的访问方式二:_类型__私有属性名
例4:
class Test:
def __init__(self, name, age):
self.__name = name # 私有属性,外部不能直接调用
self.age = age #非私有属性,外部可以直接调用
def instance_func1(self):
print('instance_func1: my name is ', self.__name) #私有属性可以通过方法调用
print('instance_func1: my age is ', self.age) #非私有属性也可以通过方法调用
test1 = Test('sunny', 18)
print(test1.__name) # error,私有属性外部不可以访问
print(test1.age) #right,非私有属性外部可以访问
test1.instance_func1() # 私有属性的访问方式一:可以通过方法访问私有属性
print(dir(test1)) #查看实例的所有外部可访问属性和方法
print(test1._Test__name) # 私有属性的访问方式二:_类型__私有属性名
5. 类的继承
(1)提高程序复用性
(2)python支持多继承
(3)不写时默认继承自object,所有class类都继承自object
例5:
class Person(object): #继承自object
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print('my name is {0}, my age is {1}'.format(self.name, self.age))
class Student(Person): #继承自Person
def __init__(self, name, age, student_num):
super().__init__(name, age) #继承Person的初始化方法
self.student_num = student_num
def info(self): #方法的重写
super().info() #继承父类的方法
print('my student number is ', self.student_num)
stu = Student('sunny', 18, 1001)
stu.info() #调用继承自父类的info方法
6. 类的多态
(1)多态:多个子类继承自同一个父类,都重写父类方法,表现出同一方法呈现不同行为的现象。
(2)提高代码的可扩展性
(3)多态实现的三个必要步骤:继承/方法重写/父类引用指向子类对象
例6:
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print('my name is {0}, my age is {1}'.format(self.name, self.age))
class Student(Person):
def __init__(self, name, age, student_num):
super().__init__(name, age)
self.student_num = student_num
def info(self):
super().info()
print('my student number is ', self.student_num)
class Teacher(Person):
def __init__(self, name, age, teach_num):
super().__init__(name, age)
self.teach_num = teach_num
def info(self):
super().info()
print('my teach number is ', self.teach_num)
stu = Student('sunny', 18, 1001)
stu.info()
teach = Teacher('Julie', 33, 12)
teach.info()
7. 类的特殊属性和方法
(1)__dict__:获得类对象或实例对象所绑定的所有属性和方法的字典
(2)__len__:通过重写该方法,让该内置函数的参数可以是自定义类型
(3)__add__:通过重写该方法,可使用自定义对象具有‘+‘的功能
(4)__new__:用于创建对象
(5)__init__:对创建的对象初始化
例7:
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name):
self.name = name
def __add__(self, other):
return self.name + other.name
def __len__(self):
return len(self.name)
c = C('sunny')
f = C('raniy')
g = c + f
print(g)
h = c.__add__(f)
print(h)
print(c.__len__())
print(c.__dict__) #属性字典
print(C.__dict__) #属性和方法字典
print(c.__class__) #该实例所属的类
print(C.__bases__) #显示所有父类
print(C.__base__) #只显示第一个父类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #显示子类
a = 2
b = 10
e = a + b #两个对象相加,实际调用的是__add__方法
d = a.__add__(b)
print(e)
print(d)
list = [1,2,3,4]
print(len(list))
print(list.__len__())