Python是一门面向对象的语言,对于面向对象语言来说,最重要的概念就是类(Class)和实例(Intance),所以python在创建一个类或者一个对象时很容易的。
类(Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
1
2
3
|
class
ClassName:
"""类的说明"""
类的内容
|
【类的内容可以写类的全局变量,方法等】
举个例子:
1
2
3
4
5
6
7
8
9
10
11
|
class
ren(
object
):
'''this is a new class'''
name
=
'meinv'
sex
=
'woman'
a
=
ren()
print
(
type
(a))
print
(a.name)
print
(a.sex)
a.age
=
10
print
(a.age)
|
结果:
<class '__main__.ren'>
meinv
woman
10
解释:
1.object默认是所有类的父类,不写默认继承object
2.a = ren(),就是把类ren实例化,
3.以上打印a的类型就是一个ren的类
4.调用类的方法和变量,直接实例化类的后面直接用“.”调用就可以。
5.如果想给实例a添加变量或者赋值,可以直接用“.”加变量赋值就可以了
2.类的构造器
__init__ 构造函数,在生成对象时调用。由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。
通过定义一个特殊的__init__方法,在创建实例的时候,就把name,score等属性绑上去:
1
2
3
4
|
class
Student(
object
):
def
__init__(
self
, name, score):
self
.name
=
name
self
.score
=
score
|
注意到__init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。
有了__init__方法,在创建实例的时候,就不能传入空的参数了,必须传入与__init__方法匹配的参数,但self不需要传,Python解释器自己会把实例变量传进去:
1
2
3
4
5
6
7
|
class
Student(
object
):
def
__init__(
self
, name, score):
self
.name
=
name
self
.score
=
score
s
=
Student(
'ling'
,
89
)
print
(s.name)
print
(s.score)
|
结果:
ling
89
解释:
1,在传递参数的时候,必须是传递两个参数,name和score,不然报错
2,Self的参数不用传递,python自动会把Student实例化的s传递给第一个参数,即self
3.类的继承
有一个现有的A类,现在需要写一个B类,但是B类是A类的特殊版,我们就可以使用继承,B类继承A类时,B类会自动获得A类的所有属性和方法,A类称为父类,B类陈为子类,子类除了继承父类的所有属性和方法,还可以自定义自己的属性和方法,大大增加了代码的复用性。
继承类的格式:
1
2
|
class
A(父类):
…
|
Python的类支持多继承,而java没有多继承,但是可以有多接口的实现python的多继承很简单,下面来看一下多继承的格式:
1
2
3
4
5
6
7
8
|
class
A:
# 定义类 A
.....
class
B:
# 定义类 B
.....
class
C(A, B):
# 继承类 A 和 B
.....
|
多继承其实在需要在父类的位置,直接写多个父类就可以,然后用“,”分开就可以了,C类就同时继承了A类和B类。
Python类的继承注意事项:
1.在继承中类的构造(__init()方法)不会自动调用,它需要在子类的构造中亲自调用。
2.Python总是首先子类中的方法,如果子类没有找到,才回去父类中查找。
例子1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class
parent:
name
=
'parent'
age
=
100
def
__init__(
self
):
print
(
'my name is parent'
)
def
get_name(
self
):
return
self
.name
def
get_age(
self
):
return
self
.age
class
child(parent):
def
__init__(
self
):
print
(
'my name is clild'
)
def
hello(
self
):
print
(
'hello child'
)
a
=
child()
a.hello()
print
(a.get_name())
print
(a.get_age())
|
结果:
my name is clild
hello child
parent
100
例子2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class
parent:
name
=
'parent'
age
=
100
def
__init__(
self
):
print
(
'my name is parent'
)
def
get_name(
self
):
return
self
.name
def
get_age(
self
):
return
self
.age
class
child(parent):
def
hello(
self
):
print
(
'hello child'
)
a
=
child()
a.hello()
print
(a.get_name())
print
(a.get_age())
|
结果:
my name is parent
hello child
parent
100
解释:
以上两个例子,如果子类没有定义__init__()方法,子类初始化的时候就会调用父类的方法,但是当子类定义了__init__()方法,子类就不会调用父类的__init__()方法,那如果子类想实现父类构造器中的方法,然后自己在写自己特殊的方法呢,这样该怎么办呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
super
()
class
parent(
object
):
name
=
'parent'
age
=
100
def
__init__(
self
):
print
(
'my name is parent'
)
def
get_name(
self
):
return
self
.name
def
get_age(
self
):
return
self
.age
class
child(parent):
def
__init__(
self
):
super
(child,
self
).__init__()
print
(
'my name is clild'
)
def
hello(
self
):
print
(
'hello child'
)
a
=
child()
a.hello()
print
(a.get_name())
print
(a.get_age())
|
结果:
my name is parent
my name is clild
hello child
parent
100
解释:
这时候就出现了子类调用父类的__init__()的方法
注意:
这里在class parent(object):定义父类的时候,一定要写继承object类,
不然那就会有如下的报错信息:
TypeError: super() argument 1 must be type, not classobj
增加 parent(object) 就可以轻松解决。