码迷,mamicode.com
首页 > 编程语言 > 详细

Python 面向对象

时间:2016-05-15 19:35:10      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

继承 案例 直接看代码

技术分享
#!/usr/bin/env python
#_*_ coding:utf-8 _*
__author__ = Administrator


#继承

class SchoolMember(object):
    member_nums = 0
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
        self.enroll()
        #self.tell()

    def enroll(self):
        SchoolMember.member_nums += 1
        print("\033[32mThe [%s] school member  [%s] is enrolled\033[0m"% (self.member_nums,self.name))



    def tell(self):
        print("Hello my name is %s" % self.name)

class Teacher(SchoolMember):
    def __init__(self,name,age,sex,course,salary): #先重写父类
        super(Teacher,self).__init__(name,age,sex)
        self.course = course
        self.salary = salary

    def teaching(self):
        print("Teacher [%s] is teaching [%s]" %(self.name,self.course))


class Student(SchoolMember):
    def __init__(self,name,age,sex,course,tution):
        super(Student,self).__init__(name,age,sex)
        self.course = course
        self.tution = tution
    def pay_tution(self):
        print("cao,student [%s] paying  tution [%s]"% (self.name,self.tution))


t1 = Teacher("yefei",25,"F","PY",1000)
t2 = Teacher("wusiqi",23,"F","PY",1500)

s1 = Student("lisi",22,"F","python",15000)
s2 = Student("zhangsan",23,"F","python",5000)

t1.tell()
t1.teaching()

s1.tell()
s1.pay_tution()
View Code

 

Python 面向对象

标签:

原文地址:http://www.cnblogs.com/yexiaochong/p/5495804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!