• 使用一个例子去理解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的概念

  • 相关阅读:
    【C++】资源管理
    【Shell脚本】逐行处理文本文件
    【算法题】rand5()产生rand7()
    【Shell脚本】字符串处理
    Apple iOS产品硬件参数. 不及格的程序员
    与iPhone的差距! 不及格的程序员
    iPhone游戏 Mr.Karoshi"过劳死"通关. 不及格的程序员
    XCode V4 发布了, 苹果的却是个变态. 不及格的程序员
    何时readonly 字段不是 readonly 的?结果出呼你想象!!! 不及格的程序员
    object file format unrecognized, invalid, or unsuitable Command 不及格的程序员
  • 原文地址:https://www.cnblogs.com/sunchao1984/p/5145404.html
Copyright © 2020-2023  润新知