python学习 继承

学习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)

目前只写了这么多,还没有理解外部添加的属性,后期再学习

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值