面向对象 6组合&抽象类&多态与多态性

#组合
class People:
    school='luffycity'

    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(Teacher, self).__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(Student, self).__init__(name,age,sex)

        self.class_time=class_time

    def learn(self):
        print('%s is learning'%self.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 Date:
    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))


# teacher1=Teacher('alex',18,'male',10,3000)
# teacher2=Teacher('egon',18,'male',30,3000)
# python = Course('python',2000,'3mons')
# linux=Course('linux',3000,'4mons')
# print(python.course_name)

# teacher1.course=python
# teacher2.course=python


# print(python)
# print(teacher1.course)
# print(teacher2.course)

# teacher1.course.tell_info()

# student1=Student('张三',29,'female','00:09:00')
# student1.course1=python
# student1.course2=linux

# student1.course1.tell_info()
# student1.course2.tell_info()
# student1.courses=[]
# student1.courses.append(python)
# student1.courses.append(linux)

student1=Student('张三',29,'female','00:09:00')
d=Date(1998,2,30)
python = Course('python',2000,'3mons')


student1.birth=d
student1.birth.tell_info()

student1.course=python
student1.course.tell_info()

抽象类

import abc

class Animal(metaclass=abc.ABCMeta):#只能被继承,不能实例化
    all_type='animal'

    @abc.abstractmethod
    def run(self):
        pass
    @abc.abstractmethod
    def eat(self):
        pass
# animal = Animal()

class People(Animal):
    def run(self):
        print('people is running')

    def eat(self):
        print('people is eating')


class Pig(Animal):
    def run(self):
        print('pig is running')

    def eat(self):
        print('pig is eating')

class Dog(Animal):
    def run(self):
        print('dog is zouing')

    def eat(self):
        print('dog is eating')

peo1=People()
pig1=Pig()
dog1=Dog()

peo1.eat()
pig1.eat()
dog1.eat()

print(peo1.all_type)

多态

#多态:同一种事物的多种形态
import abc

class Animal(metaclass=abc.ABCMeta):#只能被继承,不能实例化

    @abc.abstractmethod
    def run(self):
        pass

class People(Animal):
    def run(self):
        print('people is running')

class Pig(Animal):
    def run(self):
        print('pig is running')

class Dog(Animal):
    def run(self):
        print('dog is zouing')

class Cat(Animal):
    def run(self):
        print('cat is zouing')

#多态性:指的是在可以不考虑对象的类型的情况下而直接使用对象
peo1=People()
pig1=Pig()
dog1=Dog()
cat1=Cat()

def func(animal):
    animal.run()

func(peo1)
func(pig1)
func(dog1)
func(cat1)

在这里插入图片描述

鸭子类型

#
#
# class File:
#     def read(self):
#         pass
#
#     def write(self):
#         pass
#
# class Disk:
#     def read(self):
#         print('disk read')
#
#     def write(self):
#         print('disk write')
#
# class Text:
#     def read(self):
#         print('text read')
#
#     def write(self):
#         print('text write')
#
# disk =Disk()
# text=Text()
#
# disk.read()
# disk.write()
#
# text.write()
# text.read()

#序列类型:列表list 、元组tuple、字符串str

l=list([1,2,3])
t=tuple(('a','b''c'))
s=str('hello')

print(l.__len__())
print(t.__len__())
print(s.__len__())

def len(obj):
    return obj.__len__()

print(len(l))
print(len(t))
print(len(s))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值