這次的項目稍微長一點,分成兩篇文章來寫
我們來簡單的模擬一個手機操作系統(tǒng)
這里我們將項目分成了兩個模塊
分別是手機本體和手機聯(lián)系人
本篇我們看一下手機聯(lián)系人的模塊
這里我們先定義餓了聯(lián)系人類
然后我們開始構造初始類屬性
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
添加聯(lián)系人
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
清空聯(lián)系人列表
def clear_chat_history(self):
self.messages = []
return True
獲取最后添加聯(lián)系人和獲取最先添加的聯(lián)系人
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
將聯(lián)系人列表拷貝一份
def create_copy(self):
new_contact = dorrigo_contact(self.fname, self.lname, self.pnumber)
new_contact.messages = self.messages
return new_contact
完整的聯(lián)系人模塊代碼如下:
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,小白指導呛谜,教學分享記得私信我