目录
第一章、简介
参考http://www.shareblogs.top/306/
类的继承、类的多态、类方法方法重写,与C#/C++类似。
第二章、类的对象
# 类
# 类的定义
class MyClass:
i = 12345
# 类的函数定义
def Method(self):
print(self) # self代表类MyClass的实例,代表当前对象的地址。self 不是 python 关键字,换成self1111也可以。
return 'hello world'
# 类的默认构造函数
def __init__(self1111, realpart, imagpart):
print(self1111) # self代表类MyClass的实例,代表当前对象的地址。self 不是 python 关键字,换成self1111也可以。
self1111.aaaa1 = realpart
self1111.bbbb2 = imagpart
print("执行默认构造函数")
# 类的实例化
myClass = MyClass(100, 200)
# 访问类的属性和方法
print("------------------默认构造函数中-----------------------")
print("默认构造函数中", myClass.aaaa1, myClass.bbbb2)
print()
输出结果:
C:\Users\lanmage2\test1\Scripts\python.exe F:/百度云同步盘/我的学习/18、python/test1/View/FileTwo.py
<__main__.MyClass object at 0x000002482B4DABE0>
执行默认构造函数
------------------默认构造函数中-----------------------
默认构造函数中 100 200
------------------Method函数中-----------------------
MyClass 类的属性 i 为: 12345
<__main__.MyClass object at 0x000002482B4DABE0>
MyClass 类的方法 f 输出为: hello world
Process finished with exit code 0
第三章、类的属性与方法
3.1、类的属性
其实第二章,已经介绍了类的方法。
类的方法。需要注意几点(已经截图):
- 必须带有self参数。
- 尽量包含默认构造函数。
- 注意区分私有属性与基本属性。私有属性不能在类的外部被使用或直接访问。
3.2、类的方法
与类的私有属性一样,内的私有方法只能在类内部使用,比如默认构造函数。类的专有方法,在Python3中,常常包含:
- __init__ : 构造函数,在生成对象时调用
- __del__ : 析构函数,释放对象时使用
- __repr__ : 打印,转换
- __setitem__ : 按照索引赋值
- __getitem__: 按照索引获取值
- __len__: 获得长度
- __cmp__: 比较运算
- __call__: 函数调用
- __add__: 加运算。请单步执行调试看看。单步执行参考https://mp.csdn.net/postedit/95527362
- __sub__: 减运算
- __mul__: 乘运算
- __truediv__: 除运算
- __mod__: 求余运算
- __pow__: 乘方
__str__:当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据
class Vector:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
def __add__(self, other1):
return Vector(self.a + other1.a, self.b + other1.b)
v1 = Vector(1, 100)
v2 = Vector(2, 100)
v3 = Vector(3, 300)
v4 = Vector(4, 400)
print(v1)
print(v2)
print(v1 + v2 + v3 + v4)
输出结果:
C:\Users\lanmage2\test1\Scripts\python.exe F:/百度云同步盘/我的学习/18、python/test1/View/FileTwo.py
Vector (1, 100)
Vector (2, 100)
Vector (10, 900)
Process finished with exit code 0
第四章、类的继承
4.1、单继承
- 格式为class DerivedClassName(BaseClassName1):
- 若是子类与基类中有相同的方法名,先找子类的方法名、后找基类的方法名。
4.1.1、无重写
# 类的单继承和多继承
class People: # 类的名字,要大写
name = ''
age = 0
__weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
def __init__(self, n, a, w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" % (self.name, self.age))
# 单继承示例
class Student(People):
grade = ''
def __init__(self, n, a, w, g):
People.__init__(self, n, a, w) # 调用父类的构函
self.grade = g
s = Student('ken', 10, 60, 3)
s.speak() # 直接调用父类的方法
输出结果;
C:\Users\lanmage2\test1\Scripts\python.exe F:/百度云同步盘/我的学习/18、python/test1/View/FileTwo.py
ken 说: 我 10 岁。
Process finished with exit code 0
4.1.2、重写
# 类的单继承和多继承
class People: # 类的名字,要大写
name = ''
age = 0
__weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
def __init__(self, n, a, w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" % (self.name, self.age))
# 单继承示例
class Student(People):
grade = ''
def __init__(self, n, a, w, g):
People.__init__(self, n, a, w) # 调用父类的构函
self.grade = g
# 覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))
s = Student('ken', 10, 60, 3)
s.speak() # 调用子类的方法
输出结果:
C:\Users\lanmage2\test1\Scripts\python.exe F:/百度云同步盘/我的学习/18、python/test1/View/FileTwo.py
ken 说: 我 10 岁了,我在读 3 年级
Process finished with exit code 0
4.2、多继承
- 多继承的格式为class DerivedClassName(Base1, Base2, Base3):
- 若是子类与基类中有相同的方法名,先找子类的方法名、后找基类(从左向右)的方法名。
# 类的多继承
class People:
# 定义基本属性
name = ''
age = 0
# 定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
# 定义构造方法
def __init__(self, n, a, w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" % (self.name, self.age))
# 单继承示例
class Student(People):
grade = ''
def __init__(self, n, a, w, g):
# 调用父类的构函
People.__init__(self, n, a, w)
self.grade = g
# 覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))
# 另一个类,多重继承之前的准备
class Speaker():
topic = ''
name = ''
def __init__(self, n, t):
self.name = n
self.topic = t
def speak(self):
print("我叫 %s,我是一个演说家,我演讲的主题是 %s" % (self.name, self.topic))
# 多重继承
class Sample(Speaker, Student):
a = ''
def __init__(self, n, a, w, g, t):
Student.__init__(self, n, a, w, g)
Speaker.__init__(self, n, t)
test = Sample("Tim", 25, 80, 4, "Python")
test.speak() # 方法名同,默认调用的是在括号中排前地父类的方法
输出结果:
C:\Users\lanmage2\test1\Scripts\python.exe F:/百度云同步盘/我的学习/18、python/test1/View/FileTwo.py
我叫 Tim,我是一个演说家,我演讲的主题是 Python
Process finished with exit code 0
第五章、类的方法重写
请参考4.1