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

python 学习_第三模块 面向对象(中级)

时间:2019-04-03 18:03:58      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:people   tac   running   init   bst   pig   使用   level   data   

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 学习_第三模块 面向对象(中级)

标签:people   tac   running   init   bst   pig   使用   level   data   

原文地址:https://www.cnblogs.com/augustyang/p/10650439.html

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