Python类一
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。
class ClassName:
'''类的说明'''
类的内容
举例:
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
类的构造器
class ren(object):
def __init__(self, name, sex):
self.name = name
self.sex = sex
def hello(self):
print ('Hello {0}'.format(self.name))
test = ren("Kelake", "M")
test.hello()
test2 = ren("King", "M")
test2.hello()
运行结果:
Connected to pydev debugger (build 172.3968.37)
Hello Kelake
Hello King
Process finished with exit code 0
类的继承
class A(object):
pass
class B(object)
pass
class C(A,B)
pass
类C继承类A和类B .