zoukankan      html  css  js  c++  java
  • 面向对象

    1 、接口类,抽象类

    from abc import ABCMeta,abstractmethod
    class Payment(metaclass=ABCMeta): 抽象类(接口类):
      @abstractmethod
      def pay(self): pass 制定了一个规范
      @abstractmethod
      def func(self):pass
    class Alipay(Payment):
      def __init__(self,money):
        self.money = money
      def pay(self):
        print('使用支付宝支付了%s' %self.money)
    class Jdpay(Payment):
      def __init__(self, money):
        self.money = money
      def pay(self):
        print('使用京东支付了%s' % self.money)
    class Wechatpay(Payment):
      def __init__(self,money):
        self.money = money
      def pay(self):
        print('使用微信支付了%s' % self.money)
    def pay(obj):
      obj.pay()
    w1 = Wechatpay(200)
    a1 = Alipay(200)
    j1 = Jdpay(100)
    pay(a1) 归一化设计
    w1 = Wechatpay(300)
    w1.weixinpay()

    2、属性

    class Person:
    def __init__(self,name,age):
    self.name = name
    if type(age) is int:
    self.__age = age
    else:
    print('你输入的年龄的类型有误,请输入数字')
    @property
    def age(self):
    return self.__age
    @age.setter
    def age(self,a1):
    if type(a1) is int:
    self.__age = a1
    else:
    print('你输入的年龄的类型有误,请输入数字')
    @age.deleter
    def age(self):
    del self.__age
    p1 = Person('alex',2)
    # print(p1.__dict__)
    print(p1.age)
    p1.age = 30
    print(p1.age)
    # del p1.age
    # print(p1.__dict__)

    @property    啥时用:类似于计算周长、面积

    @age.setter   更改私有变量属性值

    @age.deleter  删除私有变量属性

    3、反射

    class Manager:
    OPERATE_DIC = [
    ('创造学生账号', 'create_student'),
    ('创建课程', 'create_course'),
    ('查看学生信息', 'check_student_info'),
    ]
    def __init__(self,name):
    self.name = name
    def create_student(self):
    print('创造学生账号')
    def create_course(self):
    print('创建课程')
    def check_student_info(self):
    print('查看学生信息')
    class Student:
    OPERATE_DIC = [
    ('查看所有课程', 'check_course'),
    ('选择课程', 'choose_course'),
    ('查看已选择的课程', 'choosed_course')
    ]
    def __init__(self,name):
    self.name = name
    def check_course(self):
    print('查看所有课程')
    def choose_course(self):
    print('选择课程')
    def choosed_course(self):
    print('查看已选择的课程')
    def login():
    username = input('name:')
    password = input('pwd:')
    with open('userinfo') as f:
    for line in f:
    usr,pwd,ident = line.strip().split('|')
    if usr ==username and pwd == password:
    print('登陆成功')
    return username,ident

    import sys
    def main():
    usr,id = login()
    print('usr,id:',usr,id)
    file = sys.modules['__main__']
    cls = getattr(file,id)
    obj = cls(usr)
    operate_dic = cls.OPERATE_DIC
    while True:
    for num,i in enumerate(operate_dic,1):
    print(num,i[0])
    choice = int(input('num<<<'))
    choice_item = operate_dic[choice-1]
    getattr(obj,choice_item[1])()
    main()
  • 相关阅读:
    利用@media screen实现网页布局的自适应
    心得体悟帖---200315(任何时候,都不要为不值得的人,不值得的事,费时间,费心力)
    心得体悟帖---200315(急啥,复习什么录什么)
    vue项目目录结构详解
    日常英语---200313(npm WARN deprecated vue-cli@2.9.6: This package has been deprecated in favour of @vue/cli)
    vuex是干什么的
    石川es6课程---4、箭头函数
    石川es6课程---3、变量let和常量const
    vue参考---eslink编码规范检查
    vue参考---vue项目结构
  • 原文地址:https://www.cnblogs.com/mengxiangqun/p/11113194.html
Copyright © 2011-2022 走看看