美文网首页
用Python模拟一个手机操作系统——手机联系人模块的实现

用Python模拟一个手机操作系统——手机联系人模块的实现

作者: Miku丨无形 | 来源:发表于2021-01-02 14:08 被阅读0次

    这次的项目稍微长一点,分成两篇文章来写
    我们来简单的模拟一个手机操作系统

    这里我们将项目分成了两个模块
    分别是手机本体和手机联系人

    本篇我们看一下手机联系人的模块

    这里我们先定义饿了联系人类
    然后我们开始构造初始类属性

    def __init__(self, fname, lname, pnumber):
        self.fname = fname
        self.lname = lname
        self.pnumber = pnumber
        self.messages = []
        self.m_messages = 20
    

    接下来是基础的获取和修改基础信息

    def get_first_name(self):
        return self.fname
    
    def get_last_name(self):
        return self.lname
    
    def get_phone_number(self):
        return self.pnumber
    
    def update_first_name(self, first_name):
        if first_name:
            self.fname = first_name
        else:
            return None
    
    def update_last_name(self, last_name):
        if last_name:
            self.lname = last_name
        else:
            return None
    
    def update_phone_number(self, number):
        if number:
            s_number = str(number)
            if 6 <= len(s_number) <= 14:
                if ' ' not in s_number and '+' not in s_number:
                    self.pnumber = number
                    return True
                else:
                    return False
            else:
                return False
        else:
            number = ''
            return False
    

    添加联系人

    def add_chat_message(self, who_said_it, message):
        if len(self.messages) < self.m_messages:
            self.messages.append(who_said_it + ": " + message)
            return True
        else:
            messages_copy = []
            flag = 1
            while flag < self.max_messages:
                messages_copy.append(self.messages[flag])
                flag += 1
            self.messages = messages_copy
            self.messages.append(who_said_it + ':' + message)
            return True
    

    清空联系人列表

    def clear_chat_history(self):
        self.messages = []
        return True
    

    获取最后添加联系人和获取最先添加的联系人

    def get_last_message(self):
        return self.messages[-1] if self.messages else None
    
    def get_oldest_message(self):
        return self.messages[0] if self.messages else None
    

    将联系人列表拷贝一份

    def create_copy(self):
        new_contact = dorrigo_contact(self.fname, self.lname, self.pnumber)
        new_contact.messages = self.messages
        return new_contact
    

    完整的联系人模块代码如下:

    class dorrigo_contact:
    
        def __init__(self, fname, lname, pnumber):
            self.fname = fname
            self.lname = lname
            self.pnumber = pnumber
            self.messages = []
            self.m_messages = 20
    
        def get_first_name(self):
            return self.fname
    
        def get_last_name(self):
            return self.lname
    
        def get_phone_number(self):
            return self.pnumber
    
        def update_first_name(self, first_name):
            if first_name:
                self.fname = first_name
            else:
                return None
    
        def update_last_name(self, last_name):
            if last_name:
                self.lname = last_name
            else:
                return None
    
        def update_phone_number(self, number):
            if number:
                s_number = str(number)
                if 6 <= len(s_number) <= 14:
                    if ' ' not in s_number and '+' not in s_number:
                        self.pnumber = number
                        return True
                    else:
                        return False
                else:
                    return False
            else:
                number = ''
                return False
    
        def add_chat_message(self, who_said_it, message):
            if len(self.messages) < self.m_messages:
                self.messages.append(who_said_it + ": " + message)
                return True
            else:
                messages_copy = []
                flag = 1
                while flag < self.max_messages:
                    messages_copy.append(self.messages[flag])
                    flag += 1
                self.messages = messages_copy
                self.messages.append(who_said_it + ':' + message)
                return True
    
        def clear_chat_history(self):
            self.messages = []
            return True
    
        def get_last_message(self):
            return self.messages[-1] if self.messages else None
    
        def get_oldest_message(self):
            return self.messages[0] if self.messages else None
    
        def create_copy(self):
            new_contact = dorrigo_contact(self.fname, self.lname, self.pnumber)
            new_contact.messages = self.messages
            return new_contact
    

    手机本体模块的实现

    一起学习python,小白指导,教学分享记得私信我

    相关文章

      网友评论

          本文标题:用Python模拟一个手机操作系统——手机联系人模块的实现

          本文链接:https://www.haomeiwen.com/subject/dxyeoktx.html