day06_02 继承

Posted flytoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day06_02 继承相关的知识,希望对你有一定的参考价值。

事例一

__author__ = "Alex Li"


# class People: 经典类
class People(object): #新式类
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.friends = []
        print("--doens\'t run ")
    def eat(self):
        print("%s is eating..." % self.name)
    def talk(self):
        print("%s is talking..." % self.name)
    def sleep(self):
        print("%s is sleeping..." % self.name)

#关系
class Relation(object):
    # def __init__(self,n1,n2):
    #     print("init in relation")
    def make_friends(self,obj): #w1
        print("%s is making friends with %s" % (self.name,obj.name))
        self.friends.append(obj.name)

#继承父类
# class Man(People):
#多继承
class Man(Relation,People):
    #重构构造函数
    def __init__(self,name,age,money):
        # People.__init__(self,name,age)    #经典类
        super(Man,self).__init__(name,age) #新式继承父类写法,不用记住父类名字更方便
        self.money  = money
        print("%s 一出生就有%s money" %(self.name,self.money))
    def piao(self):
        print("%s is piaoing ..... 20s....done." % self.name)
    #重构父类
    def sleep(self):
        People.sleep(self)
        print("man is sleeping ")

# class Woman(People,Relation):
class Woman(People):
    def get_birth(self):
        print("%s is born a baby...." % self.name)

m1 = Man("NiuHanYang",22,10)
# m1.eat()
# m1.piao()
# m1.sleep()

w1 = Woman("ChenRonghua",26)
# w1.get_birth()


m1.make_friends(w1) #继承People析构初始化
w1.name = "陈三炮"
print(m1.friends[0])
m1.make_friends(w1)
print(m1.friends[0],m1.friends[1])

事例二

__author__ = "Alex Li"

class School(object):
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
        self.students =[]
        self.staffs =[]
    def enroll(self,stu_obj):
        print("为学员%s 办理注册手续"%stu_obj.name )
        self.students.append(stu_obj)
    def hire(self,staff_obj):
        self.staffs.append(staff_obj)
        print("雇佣新员工%s" % staff_obj.name)

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def tell(self):
        pass

class Teacher(SchoolMember):
    def __init__(self,name,age,sex,salary,course):
        super(Teacher,self).__init__(name,age,sex)
        self.salary = salary
        self.course = course
    def tell(self):
        print(\'\'\'
        ---- info of Teacher:%s ----
        Name:%s
        Age:%s
        Sex:%s
        Salary:%s
        Course:%s
        \'\'\'%(self.name,self.name,self.age,self.sex,self.salary,self.course))

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

class Student(SchoolMember):
    def __init__(self,name,age,sex,stu_id,grade):
        super(Student,self).__init__(name,age,sex)
        self.stu_id = stu_id
        self.grade = grade
    def tell(self):
        print(\'\'\'
        ---- info of Student:%s ----
        Name:%s
        Age:%s
        Sex:%s
        Stu_id:%s
        Grade:%s
        \'\'\' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))
    def pay_tuition(self,amount):
        print("%s has paid tution for $%s"% (self.name,amount) )


school = School("老男孩IT","沙河")

t1 = Teacher("Oldboy",56,"MF",200000,"Linux")
t2 = Teacher("Alex",22,"M",3000,"PythonDevOps")

s1 = Student("ChenRonghua",36,"MF",1001,"PythonDevOps")
s2 = Student("徐良伟",19,"M",1002,"Linux")


t1.tell()
s1.tell()
school.hire(t1)
school.enroll(s1)
school.enroll(s2)

print(school.students)
print(school.staffs)
school.staffs[0].teach()

for stu in school.students:
    stu.pay_tuition(5000)

以上是关于day06_02 继承的主要内容,如果未能解决你的问题,请参考以下文章

day06-jsp

day06_02

day06_02 元组

day06_04 购物车讲解02

基础DAY8-继承

java小白训练营day06-OOP:面向对象+封装+继承+多态