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

Python 继承和组合 接口

时间:2017-08-15 17:20:27      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:sel   span   eve   python   foo   sed   好处   pass   inf   

技术分享
#解决代码重用的问题,减少代码冗余
#继承是一种什么‘是’什么的关系
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(egon,18)
# print(t.name,t.age)
# print(t.__dict__)
# t.walk()

# s=Student(alex,13)
# 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,egon,18,male,10,3000)
    def __init__(self, name, age,sex,level,salary):
        People.__init__(self,name,age,sex)
        #People.__init__(t,egon,18,male)
        #t.name=egon
        #t.age=18
        #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(egon,18,male,10,3000) #__init__(t,egon,18,male,10,3000)
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(egon,18,1990,2,33)
# 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(1990,2,33)
# t=Teacher(egon,18,t_birth)
组合
技术分享接口

 

Python 继承和组合 接口

标签:sel   span   eve   python   foo   sed   好处   pass   inf   

原文地址:http://www.cnblogs.com/huchong/p/7365961.html

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