python 学习_第三模块 面向对象(中级)
Posted augustyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 学习_第三模块 面向对象(中级)相关的知识,希望对你有一定的参考价值。
1.组合
# # 组合的使用 class People: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex class Teacher(People): def __init__(self,name,age,sex,level,salary): super().__init__(name,age,sex) self.level = level self.salary = salary def teach(self): print("%s is teaching" % self.name) class Student(People): def __init__(self,name,age,sex,class_time): super().__init__(name,age,sex) self.class_time = class_time def learn(self): print("%s is learning " % self.name) class School: def __init__(self,school_addr,school_name): self.school_addr = school_addr self.school_name = school_name def tell_info(self): print(‘学校名为 <%s> 学校地址为 <%s>‘ % (self.school_addr, self.school_name)) class Course: def __init__(self,course_name,course_price,course_period): self.course_name = course_name self.course_price = course_price self.course_period = course_period def tell_info(self): print(‘课程名<%s> 课程价钱<%s> 课程周期<%s>‘ % (self.course_name, self.course_price, self.course_period)) class Data: def __init__(self,year,mon,day): self.year = year self.mon = mon self.day = day def tell_info(self): print(‘%s-%s-%s‘ %(self.year,self.mon,self.day)) t = Teacher(‘alex‘,28,‘man‘,10,3000) py1 = Course(‘python‘,‘13000‘,‘3 month‘) linux1 = Course(‘linux1‘,‘11000‘,‘2 month‘) d = Data(2019,4,3) s = Student(‘yangyang‘,18,‘man‘,‘08:30:00‘) s.birh=d s.birh.tell_info() # 2019-4-3 s.course=py1 s.course.tell_info() # 课程名<python> 课程价钱<13000> 课程周期<3 month>
2.抽象类 接口继承
import abc class Animal(metaclass=abc.ABCMeta): #只能被继承,不能被实例化 all_type=‘animal‘ @abc.abstractmethod def run(self): pass @abc.abstractmethod def eat(self): pass class People(Animal): def run(self): print(‘people is running‘) def eat(self): print(‘people is eating‘) class Pig(Animal): def run(self): print(‘people is walking‘) def eat(self): print(‘people is eating‘)
3.多态
4.封装
以上是关于python 学习_第三模块 面向对象(中级)的主要内容,如果未能解决你的问题,请参考以下文章