学习python的过程中对继承的一个测试,目的是初始化子类对象,并在外部修改属性和调用子类方法访问父类对象成员,以下是测试代码。
# !usr/bin/env python
# -*- coding:utf-8 -*-
country = "英国"
class school(object):
country = '中国'
def __init__(self, name, address, number):
self.name = name
self.address = address
self.number = number
def which_(self, name, address, number, country):
print("名字叫{0},地址是{1},编号是{2},".format(self.name, self.address, self.number) + "在%s" % country)
def where_(self, country):
print("国家叫%s" % country)
def prtob(self, si_):
print(self.si_)
def changecty(self,a):
country = a
print(country)
class student(school):
height = 175
def __init__(self, name, address, number, __name, age, score):
school.__init__(self, name, address, number)
self.__name = __name
self.age = age
self.score = score
def who_(self, name, height, age, score):
print("{0}的身高是{1}厘米,年龄是{2}岁,成绩是{3}分".format(self.__name, height, self.age, self.score))
def getfatt(self, name, address):
print("{0}的学校在{1}".format(self.__name, self.address))
def addage(a, b):
return a + b
def write_obj(list, object):
s = school(list[0], list[1], list[2])
return s
def write_stu(list, dict, student):
s = student(list[0], list[1], list[2], dict['name'], dict['age'], dict['score'])
return s
if __name__ == '__main__':
my_info = ['第一小学', '南大街', 53201]
a = write_obj(my_info, school)
print(a.address)
my_student = {'name':"小李", "age":20, "score" : 100}
b = write_stu(my_info, my_student, student)
b.getfatt(b.name, b.address)
print(addage(b.age, b.age))
a.si_ = int(50)
a.prtob(a.si_) #这是无法访问的,因为si_属性是在外部添加的,并不是在__init__函数中添加的
a.country = '美国'
print(a.country, b.country)
b.country = '俄国'
print(b.country)
目前只写了这么多,还没有理解外部添加的属性,后期再学习