类的重写:
需要重写的类方法,只要在需要的子类下面重新定义即可。
class parent(object):
parent_name = 'parent'
age = 200
def __init__(self, address, sex):
self.address = address
self.sex = sex
print('my name is {0}'.format(self.parent_name))
def get_name(self):
print('parent parent parent***text jicheng parent get_name********')
return self.parent_name
def get_age(self):
return self.age
class child(parent):
child_name = 'child'
# age = 20
def __init__(self, address, sex):
# parent.__init__(self, address, sex) \\这里是关于继承__init__的两种方法。注意两者书写区别。
super(child, self).__init__(address, sex) \\两者的区别就是,使用super会自动识别父类名称,无需手动修改等
print ('my name is {0}'.format(self.child_name))
def hello(self):
print('hello world')
def get_name(self):
super(child, self).get_name() \\除了__init__这种特殊的外,普通变量也可以使用
print('rewrite lei.') \\除了父类中的方法,还可以添加些子类自己的参数等,由a.get_name返回值看出来
a = child('beijing', 'W')
a.hello()
a.get_name() \\这里的返回值为child中定义的,结合parent中定义的return来看,应该是方法直接默认忽略return的值而返回child的parint的定义值
print(a.get_age())
print(a.address)
print(a.sex)
my name is parentmy name is child
hello world
parent parent parent***text jicheng parent get_name********
rewrite lei.
200
beijing
W
对于初始化__init__的继承:在一般情况下__init__是不继承的,每个类只走自己的__init__。但是当有需求子类需要调用父类的__init__sh
class parent(object):
parent_name = 'parent'
age = 200
def __init__(self, address, sex):
self.address = address
self.sex = sex
print('my name is {0}'.format(self.parent_name))
def get_name(self):
return self.parent_name
def get_age(self):
return self.age
class child(parent):
child_name = 'child'
# age = 20
def __init__(self, address, sex):
# parent.__init__(self, address, sex) \\这里使用了两种方法实现继承父类__init__
super(child, self).__init__(address, sex)
print ('my name is {0}'.format(self.child_name))
def hello(self):
print('hello world')
def get_name(self):
print('rewrite lei.')
a = child('beijing', 'W')
a.hello()
a.get_name()
print(a.get_age())
print(a.address)
print(a.sex)
my name is parentmy name is child
hello world
rewrite lei.
200
beijing
W
类的私有变量和私有方法:
在python中,通过再属性变量名前加上双下划线定义属性为私有变量。
特殊变量命名:
_xx单下划线开头的表示的是protected类型的变量,即保护类型只能允许其本身与子类进行访问,若内部变量表示,如,当使用“from M import”时,不会将以一个下划线开头的对象引入;
__xx双下划线开头表示私有类型的变量,只能允许这个类本身进行访问,连子类也不可以用于命名一个类属型(类变量),调用时名字被改变(在类FooBar内部,__boo变成_FooBar__boo,如self.__FooBar__boo)
__xx__定义的是特列方法,用户控制的命名空间内的变量或是属性,如init,__import__或是file,只有当文档有说明时使用,不要自己定义这类变量(就是说这些是python内部定义的变量名)
class A(object):
_name ='xiao'
__sex = 'W' \\只能在class这个类中使用
def hello(self):
print(self._name)
print(self.__sex)
a = A()
print(a._name)
print(a.__sex) \\当类私有化之后,则不能这样使用,报错如下。
xiaoTraceback (most recent call last):
File "C:/Users/xiaojingjing/PycharmProjects/untitled1/siyoulei.py", line 19, in <module>
print(a.__sex)
AttributeError: 'A' object has no attribute '__sex'
class A(object):
_name ='xiao' \\实例化之后可以用
__sex = 'W' \\只能在类里面用
def hello(self):
print(self._name)
print(self.__sex)
a = A()
print(a._name)
a.hello() \\通过调用类中定义的hello函数的方式,使用私有类
xiaoxiao
W
def get_sex(self): \\上面重复部分不赘述
return self.__sex
a = A()
pirnt(a.get_sex()) \\如果有特殊情况需要再次调用__sex则可以用这种方式调用
W
python那种还有些内部定义的变量,在使用时,需要注意。