1.一个最简单的类
class Person:
def __init__(self, name, age, hobby): # 类的构造函数
self.name = name
self.age = age
self.hobby = hobby
def show_person(self):
print(self.name, self.age, self.hobby)
# 类的创建
example = Person('Alex', 20, 'girls')
example.show_person()
2. 类的继承属性形成重载
2.1 简单的继承
class Father:
def hobby(self):
print("basketball")
def haircolor(self):
print("black")
class Son(Father):
def hobby(self):
print("girls")
f = Father()
s = Son()
f.hobby()
f.haircolor()
s.hobby() # 访问自身属性
s.haircolor() # 继承父类属性
2.2 在子类中主动调用父类的方法
class Father:
def hobby(self):
print("basketball")
def haircolor(self):
print("black")
class Son(Father):
#通过重写覆盖父类的方法
def hobby(self):
super(Son, self).hobby() # 执行一次父类的hobby方法
Father.hobby(self) # 执行一次父类方法,self指代子类的对象
print("girls") # 执行子类的方法
f = Father()
s = Son()
f.hobby()
f.haircolor()
s.hobby() # 访问自身属性
s.haircolor() # 继承父类属性
2.3 多继承
方法的查找顺序:
1.沿着左继承往上找(不去查找公共的基类)
2.沿着右继承进行查找(会去查找公共的基类)
class Base:
def foo1(self):
print('Base.foo')
class F1(Base):
def foo(self):
print('F1.foo')
class F2(Base):
def foo1(self):
print('F2.foo')
class Son(F1, F2):
pass
obj = Son()
obj.foo1() # 输出 F2.foo
self 永远指代调用方法的原始对象:
class Base:
def foo1(self):
print('Base.foo')
class F1(Base):
def foo(self):
print('F1.foo')
self.foo1() # self 是obj对象 即调用 obj.foo1()
class F2(Base):
def foo1(self):
print('F2.foo')
class Son(F1, F2):
pass
obj = Son()
obj.foo() # 输出 F1.foo F2.foo
2.4 类的字段和方法
# 字段
# - 普通字段:保存在对象里面
# - 静态字段:保存在类里面
# 方法
# -普通方法:保存在类里,由对象调用
# -静态方法: 使用装饰器装饰 @staticmethod
class Person:
# 静态字段
hobby = 'girls'
count = 0
def __init__(self, name):
# 普通字段
self .name = name
@staticmethod
def hair():
Person.count += 1
print(Person.count)
Tom = Person('Tom')
print(Tom.hobby)
Person.hair()
Tom.hair()
2.5 类的属性
class Foo:
@property # 用于获取值
def per(self):
print("this is a property method")
@per.setter # 用于接收值
def ser(self, val):
print(val)
@per.deleter
def der(self):
print("this data have been deleted")
f = Foo()
# 调用属性不需要加 ()
f.per
# 给属性赋值
f.ser = 10
# 调用删除属性, 注意调用的方式 del + 属性
del f.der
2.6 类的共有、私有成员
class Foo:
__v = 'func'
def __init__(self, name, age):
self.name = name # 定义公有成员
self.__age = age # 定义私有成员
def show(self): # 间接访问私有成员
print(self.__age)
print(Foo.__v)
def __f1(self): # 私有方法
print("f1")
def f2(self):
self.__f1()
print('f2')
obj = Foo('Tom', 12)
obj.show()
obj.f2()
class F:
def __init__(self, age):
self.__age = age
def show_age(self):
print(self.__age)
# 继承来的私有成员,需要通过父类的方法来进行调用
class S(F):
def show(self):
self.show_age()
2.7 特殊成员
__call__ 方法
class Foo:
def __init__(self):
print("init func has been carried out")
def __call__(self, *args, **kwargs): # 对象加() 进行调用
print("call func has been carried")
obj = Foo()
obj() # 调用call
__int__ 和 __str__ 方法
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __int__(self):
return 123
def __str__(self):
return '%s - %s' % (self.name, self.age)
obj = Foo('Tom', 18)
a = int(obj) # 执行 __int__ 方法
print(a)
print(obj) # 执行 __str__ 方法
__add__ __sub__ 等操作符方法
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def show(self):
print(self.name, self.age)
def __add__(self, other): # 定义加法
return Person(self.name, self.age + other.age)
Tom = Person('Tom', 10)
Jack = Person('Jack', 20)
Tom = Tom + Jack
Tom.show()
__del__ 析构方法 : 一般用不到
__dict__ 将对象以字典形式返回
class Person:
def __init__(self, name, age):
self.name = name
self.__age = age
def show(self):
print(self.name, self.__age)
def __add__(self, other): # 定义加法
return Person(self.name, self.age + other.age)
Tom = Person('Tom', 10)
Jack = Person('Jack', 20)
d = Tom.__dict__
print(d) # 将对象内部的成员以字典形式展示 {'name': 'Tom', '_Person__age': 10}
__getitem__ setitem__ delitem 方法
class Foo:
def __init__(self, name, age):
self.name = name
self.age = age
def __getitem__(self, item): # 以列表形式获取值 obj[item]
if item == 0:
print(self.name)
return self.name
if item == 1:
print(self.age)
return self.age
def __setitem__(self, key, value):
print(key, value)
def __delitem__(self, key):
print(key)
obj = Foo('tom', 18)
t = obj[1]