Python 继承和组合 接口

时间:2024-01-05 15:20:44
#解决代码重用的问题,减少代码冗余
#继承是一种什么‘是’什么的关系
class People:
    def __init__(self, name, age):
        # print('People.__init__')
        self.name = name
        self.age = age
    def walk(self):
        print('%s is walking' %self.name)

class Teacher(People):
    pass

class Student(People):
    pass

# t=Teacher()
# print(t.name,t.age)
# print(t.__dict__)
# t.walk()

# s=Student()
# print(s.name,s.age)
# s.walk()

#====================
class People:
    def __init__(self, name, age,sex):
        self.name = name
        self.age = age
        self.sex=sex

    def walk(self):
        print('%s is walking' % self.name)

    def foo(self):
        print('from father %s' %self.name)

class Teacher(People):
    school = '偶的博爱'
    #__init__(t,,,)
    def __init__(self, name, age,sex,level,salary):
        People.__init__(self,name,age,sex)
        #People.__init__(t,,'male')
        #t.name='egon'
        #t.age=
        #t.sex='male'

        self.level=level
        self.salary=salary

    def teach(self):
        print('%s is teaching' %self.name)

    def foo(self):
        People.foo(self)
        print('from teacher')

class Student(People):
    def __init__(self, name, age,sex,group):
        People.__init__(self, name, age, sex)
        self.group=group
    def study(self):
            print('%s is studying' %self.name)

t=Teacher(,,) #__init__(t,,,)
t.foo()

#
# class Teacher:
#     school = '偶的博爱'
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def teach(self):
#         print('%s is teaching' %self.name)
#     def walk(self):
#         print('%s is walking' %self.name)
#
#
# class Student:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#
#     def study(self):
#         print('%s is studying' %self.name)
#     def walk(self):
#         print('%s is walking' %self.name)

继承

#继承是用来创建新的类的一种方式,好处是可以减少重复代码
#继承是类与类之间的关系,是一种什么是什么的关系

#组合:是一种什么有什么的关系,也是为了减少重复代码
class People:
    def __init__(self, name, age, year, mon, day):
        self.name = name
        self.age = age
        self.birth = Date(year, mon, day)

    def walk(self):
        print('%s is walking' % self.name)

class Date:
    def __init__(self,year,mon,day):
        self.year=year
        self.mon=mon
        self.day=day

    def tell_birth(self):
        print('出生于<%s>年 <%s>月 <%s>日' % (self.year,self.mon,self.day))

class Teacher(People):
    def __init__(self, name, age, year, mon, day,level,salary):
        People.__init__(self,name,age,year,mon,day)
        self.level=level
        self.salary=salary

    def teach(self):
        print('%s is teaching' %self.name)

class Student(People):
    def __init__(self, name, age, year, mon, day,group):
        People.__init__(self,name,age,year,mon,day)
        self.group=group
    def study(self):
        print('%s is studying' %self.name)
# t=Teacher(,,,)
# print(t.name,t.age)
# print(t.birth)
# t.birth.tell_birth()
# print(t.birth.year)
# print(t.birth.mon)
# print(t.birth.day)

class Course:
    def __init__(self,name,price,period):
        self.name=name
        self.price=price
        self.period=period

    def tell_info(self):
        print('课程名<%s> 价钱<%s> 周期<%s>' %(self.name,self.price,self.period))

#=====================================
# class Date:
#     def __init__(self,year,mon,day):
#         self.year=year
#         self.mon=mon
#         self.day=day
#
#     def tell_birth(self):
#         print('出生于<%s>年 <%s>月 <%s>日' % (self.year,self.mon,self.day))
#
# class Teacher:
#     def __init__(self,name,age,birth):
#         self.name=name
#         self.age=age
#         self.birth=birth
#
#     def teach(self):
#         print('%s is teaching' %self.name)
#
# t_birth=Date(,,)
# t=Teacher(,t_birth)

组合

接口