Advanced Python-----object-oriented 5.0 (one of the three major characteristics of objects--polymorphism)

Table of contents

Foreword:

polymorphism

exercise


Foreword:

        In the last issue, we talked about inheritance in Python facing objects, but today we are talking about polymorphism. In fact, polymorphism is closely related to inheritance. In other words, polymorphism is a form of inheritance. Let’s take a look together Bar! (The last link is Python Advanced ----- Object-oriented 4.0 (one of the three major characteristics of object--inheritance)_Python Ouni Sauce's Blog-CSDN Blog )

polymorphism

        Polymorphism, polymorphism, as the name suggests, is a variety of forms. For example, the puppies born by Husky and Corgi have both Corgi's short legs and the husky's black and white coat color. This is a kind of polymorphism. Objects are defined in this way in Python: In the process of inheritance, different objects call the same method and show different forms

 Polymorphic requirements:

1. There must be class inheritance

2. The subclass must override the method of the parent class

Example 1: 

class Cat(object):
    def sound(self):
        print('喵喵喵')
    def animal(self):
        print('猫咪')
class Dog(object):
    def souund(self):
        print('汪汪汪')
    def animal(self):
        print('小狗')
class A(Dog,Cat):
    def sound(self):
        print('呜呜呜')
class B(Cat,Dog):
    def sound(self):
        print('哇哇哇')
e1=A()
e2=B()
e1.sound()
e2.sound()
#输出结果:呜呜呜
#        哇哇哇

This is the manifestation of polymorphism. The sound() method is also used, but the results are completely different. That is because the rewritten methods in the two subclasses I defined are different.

Example 2:

class Old(object):
    def __init__(self,food,clothes):
        self.food=food
        self.clothes=clothes
    def fun(self):
        print('work')
class New(Old):
    def __init__(self,name,food,clothes):
        self.name=name #重写添加的属性
        Old.__init__(self,food,clothes) #在重写的时候获取保留父类的相关属性
    def fun(self):
        super().fun() #获取保留父类的方法
        print('work hard')   #重写的方法
a=New('伊丽莎白二世','皇家美食','裙子')
b=Old('好吃的','粉色的裙子')
print(a.name,a.clothes,a.food)
print(b.food,b.clothes)
New.fun(a)
Old.fun(b)
#输出结果:
# 伊丽莎白二世 裙子 皇家美食
# 好吃的 粉色的裙子
# work
# work hard
# work

exercise

1. Restaurant order and payment
    1. Define a restaurant
    attribute: name, menu, amount Balance
    method: order, payment [Withdrawal needs to determine whether the balance is sufficient]

    2. Define a restaurant of your own
    that inherits all the properties and methods in the restaurant class

    3. Define a certain group.
    This certain group inherits all the attributes and methods of the first restaurant
     . The payment needs to charge a packing fee, and the packing fee is 5 yuan.

think first think

Answer:

class Restaurant(object):
    def __init__(self,name,money):
        self.rname='R面馆'
        self.name=name
        self.money=money
        self.menu={0:['红烧牛肉面',15],1:['过桥米线',14],2:['桂林米粉',13]}
    def showmenu(self):
        print('{} welcom to {}\n 菜单如下:'.format(self.name,self.rname))
        for key,value in self.menu.items():
            print(f'选项{key}--{value[0]}--价格:{value[1]}')

    def select(self):
        self.showmenu()
        p=int(input('choose in 0,1,2:'))
        if p in self.menu:
            self.pay(self.menu[p][1])
        else:
            print('选择错误,菜单上没有')
    def pay(self,money):
        if self.money>money:
            self.money-=money
            print(f'已支付{money}元,找回{self.money}元')
        else:
            print('钱不够')
class Myres(Restaurant):
    pass
class Meituan(Restaurant):
    def pay(self,money):
        money+=5
        if self.money>money:
            self.money-=money
            print(f'已支付{money}元,找回{self.money}元')
        else:
            print('钱不够,qiongb')
yourmoney=int(input('输入你带了多少钱:'))
user=Meituan('李信',yourmoney)
user.select()

 

Well, that's it for this issue, see you next time~

Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/129331719