欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-28 17:11:33
这次的项目稍微长一点,分成两篇文章来写我们来简单的模拟一个手机操作系统这里我们将项目分成了两个模块分别是手机本体和手机联系人本篇我们看一下手机联系人的模块这里我们先定义饿了联系人类然后我们开始构造初始类属性def __init__(self, fname, lname, pnumber): self.fname = fname self.lname = lname self.pnumber = pnumber self.messages = [] sel...

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

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

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

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

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,小白指导,教学分享记得私信我

本文地址:https://blog.csdn.net/Miku_wx/article/details/111999046