python-类思想-实现简单增删查改

class Person:
    def __init__(self,name, phone):
        self.UserInfo = {'name': name,
                         'phone': phone}


class Opt:
    def __init__(self):
        self.person = []

    def add_person(self, user_info):
        print('增加')
        self.person.append(user_info)

    def delete_person(self):
        print('删除')
        self.person.pop()

    def select_person(self, name):
        print('查找', name)
        for p in self.person:
            if p['name'] == name:
                print(p)
                break
        else:
            print("该用户未找到")

    def update_person(self, name, phone):
        print('更新name:', name, '的phone为', phone)
        for p in self.person:
            if p['name'] == name:
                p['phone'] = phone
        else:
            print("没有找到该该用户,无法更新")

    def print_person(self):
        print('打印所有用户')
        for i in self.person:
            print(i)


if __name__ == '__main__':
    p1=Person('n1', 'p1').UserInfo
    p2=Person('n2', 'p2').UserInfo
    opt=Opt()
    opt.add_person(p1)
    opt.add_person(p2)
    opt.print_person()
    opt.select_person('n2')
    opt.select_person('n3')
    opt.delete_person()
    opt.print_person()
    opt.update_person('n1','p111')
    opt.print_person()



python-类思想-实现简单增删查改_第1张图片

你可能感兴趣的:(python基础学习,python,开发语言,后端)