zoukankan      html  css  js  c++  java
  • 使用一个例子去理解CLASS,类的内容(2)

    回到他的需求:

    1,可以新增记录,2,可以查询,3,可以全部展示,4,可以删除,5,可以更新。

    那么我们先把大框架搭起来:

    class Contact(object):
    
        def __init__(self):
            pass
    
        def search_contact(self, args):
            pass
    
        def display_all(self):
            pass
    
        def update_number(self, new_number):
            pass
    
        def delete_contact(self):
            pass
    

     看,一个类,Contact,还有包括初始化在内的5个方法,已经涵盖了我们的要求了

    然后搭好所有的类

    别忘了:

    1,你要在一个list里保存所有的信息,所以要定义在class中

    2,search和display应该是classmethod

    3,update应他要求,也做成了classmethod

    class Contact(object):
        con = []
    
        def __init__(self):
            self.f = None
            self.l = None
            self.n = None
            self.a = None
            self.list = []
    
        def setup_ins(self):
            '''f as first name,l as last name,n as number,a,as address,con as all_contacts'''
            self.f = str(input('input first name:'))
            self.l = str(input('input last name:'))
            self.n = str(input('input number:'))
            self.a = str(input('input address:'))
            # print(self.f, self.l, self.n, self.a)
            if self.f == '' or self.l == '' or self.n == '' or self.a == '':
                return 0
            else:
                self.list.extend([self.f, self.l, self.n, self.a])
                Contact.con.append(self.list)
    
        @classmethod
        def search_contact(cls, args):
            if args == '':
                return 0
            else:
                for obj in Contact.con:  # first list
                    if args in obj:  # searching in second list
                        print(obj)
                        return 1
                    else:
                        print('no such a record!')
    
        @classmethod
        def display_all(cls):
            for obj in Contact.con:
                print(obj)
    
        @classmethod
        def update_number(cls, old_number, new_number):
            for obj in Contact.con:  # first list
                if old_number in obj:  # searching in second list
                    i = Contact.con.index(obj)
                    j = obj.index(old_number)
                    Contact.con[i][j] = new_number
                    return 1
                else:
                    return 0
    
        def delete_contact(self):
            i = Contact.con.index(self.list)
            Contact.con.pop(i)
    

     下面是调用的部分:

    while 1:
        print('<<<<<<<<<<<<<<<<<<<<<<make a choice>>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(1)add record>>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(2)search record>>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(3)show all record>>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(4)delete record>>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(5)update record >>>>>>>>>>>>>>>>>>>>>>>')
        print('<<<<<<<<<<<<<<<<<<<<<<(6)exit >>>>>>>>>>>>>>>>>>>>>>>')
        choice = input('make your choice(1/2/3/4/5/6):')
        # print(type(choice))  # debug
        if choice == '1':
            user_name = str(input('type a user_name:'))
            res = 1
            exec('%s = Contact()' % user_name)
            exec('res = %s.setup_ins()' % user_name)
            if res == 0:
                print('error please completely input!')
                exec('del(%s)' % user_name)
            continue
            # Contact.display_all()  # debug
        elif choice == '2':
            search_obj = str(input('what do you want to search:'))
            res = Contact.search_contact(search_obj)
            if res == 0:
                print('input something!')
            continue
    
        elif choice == '3':
            Contact.display_all()
            continue
    
        elif choice == '4':
            delete_name = str(input('who do you want to delete:'))
            exec('%s.delete_contact()' % delete_name)
            continue
    
        elif choice == '5':
            old_num = str(input('type old number:'))
            new_num = str(input('type new number:'))
            res = Contact.update_number(old_num, new_num)
            if res == 1:
                print('success!')
            else:
                print('failed!')
            continue
    
        elif choice == '6':
            print('bye!')
            break
        else:
            print('wrong input! only 1,2,3,4,5 or 6!')
            continue
    

     代码都很简单,不做过多解释,重在理解class的概念

  • 相关阅读:
    什么叫开漏输出
    PIC16F877A TIMER1计数操作
    [转载]【Alientek STM32 实验2】按键输入
    STM32学习笔记1 IO口学习
    时间“四象限”法
    CLANNAD AFTER STORY 片头曲 「铭刻时间的歌」
    正则表达式测试工具
    War3窗口限定小工具发布
    利用ffmpeg转换mp4文件
    一个类似FlashGet的c#开源下载工具
  • 原文地址:https://www.cnblogs.com/sunchao1984/p/5145404.html
Copyright © 2011-2022 走看看