博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用一个例子去理解CLASS,类的内容(2)
阅读量:5305 次
发布时间:2019-06-14

本文共 3928 字,大约阅读时间需要 13 分钟。

回到他的需求:

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('<<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>>>>') 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的概念

转载于:https://www.cnblogs.com/sunchao1984/p/5145404.html

你可能感兴趣的文章
CUDA中的流与事件
查看>>
Ubuntu环境下GitHub安装与使用
查看>>
HTTP状态码详解
查看>>
分块 公主的朋友
查看>>
2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】...
查看>>
2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】...
查看>>
BZOJ-3207 花神的嘲讽计划Ⅰ
查看>>
Unsafe与CAS
查看>>
多线程模型一:只完成最新任务
查看>>
NOIP2017游记+AFO感想
查看>>
团队作业(二):项目选题
查看>>
毕业论文排版格式)
查看>>
IIS返回的时间中含有中文问题
查看>>
虚拟地址空间
查看>>
使用Flexpaper实现仿百度文库的效果
查看>>
UVA - 1619 Feel Good(扫描法)
查看>>
selenium webdriver (4)
查看>>
MySQL数据库
查看>>
《Linux4.0设备驱动开发详解》笔记--第十四章:Linux网络设备驱动
查看>>
Java基础语法(二 )
查看>>