1.类的继承
- 仅继承类,但不使用属性
class Person(object):
_city = 'beijing'
def __init__(self, name):
self._name = name
class Student(Person):
def __init__(self,score):#init会被标黄
self.score = score
a = Student(120)
- 继承类的属性,并使用
class Person(object):
_city = 'beijing'
def __init__(self, name):
self._name = name
class Student(Person):
def __init__(self,name,score):
#必须使用super(),来初始化从父类继承过来的属性
super(Student,self).__init__(name)
self.score = score
a = Student('nan',120)
supre使用方法:
super(子类名称
,self).init(属性1,属性2,属性3
)
- 多重继承
class A(object):
def __init__(self,name):
self._name = name
class B(A):
def __init__(self,name):
super(B,self).__init__(name)
class C(A):
def __init__(self,name,score,hello):
super(C,self).__init__(name)
class D(B,C):
def __init__(self,name):
super(D,self).__init__(name)
- 判断类型
class A(object):
def __init__(self,name):
self._name = name
def a(self):
name = self._name
return name
class B(A):
def __init__(self,name):
super(B,self).__init__(name)
b = B('b')
c = b.a()
print(isinstance(b,A)) #===>True
print(type(b)) #===> <class '__main__.B'>
print(type(c)) #===> <class 'str'>
print(isinstance(c,A)) #===>False
print(type(b) == A) #===>False
print(type(b) == B) #===>True
print(type(c) == str) #===>True
# type() 不会认为子类是一种父类类型,不考虑继承关系。
# isinstance() 会认为子类是一种父类类型,考虑继承关系
补充:isinstance()与type()的区别
:print(type(c) == str) #===>True print(isinstance(c,int)) #===>True print(isinstance(c,(int,str,dict))) #===>True,符合元组中的任意一个,即为真
- 子类调用父类方法
python class A(object):
def __init__(self,name):
self._name = name
def a(self):
print('这是给一个父类方法')
class B(A):
def __init__(self,name):
super(B,self).__init__(name)
b = B('b') b.a() #===>这是给一个父类方法 ```
子类可以直接调用父类方法
2.多态
class A(object):
def a(self):
print("A")
class B(A):
def __init__(self):
pass
def a(self):
print('B')
class C(A):
def a(self):
print("C")
class D(B,C):
pass
a = A()
b = B()
c = C()
d = D()
a.a() #===>A
b.a() #===>B
c.a() #===>C
d.a() #===>B
类B继承了A的a()方法,同时自己也有a()方法,当B的对象调用a()方法时,优先选择调用自身方法,以上成为类的多态
若没有没有定义该方法则调用类方法。
3.获取类/对象信息
- 获取类/对象信息
class A(object):
def a(self):
print("A")
print("C")
a = A()
print(dir(A))
#==>['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a']
print(dir(a))
#==>['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'name']
注:
a()和name也是类和对象的属性,已知属性名称的情况下,可以通过函数获取这些属性的属性,或者修改属性的值
- 获取或修改类/对象的属性
class A(object):
def __init__(self,name):
self.name = name
def a(self):
print("A")
a = A('aaaaa')
#获取属性值
print(getattr(a,'name')) --->aaaaa
#修改属性值
setattr(a,'name','xinzeng')
print(a.name) --->xinzeng