Python-何时创建类以及何时创建函数 [英] Python - When to create a Class and when to create a Function
问题描述
对,所以我试图使用Python OOP创建一个Contacts应用程序.我对OOP还是很陌生,但仍在尝试了解这些概念.
Right, so I'm trying to create a Contacts application using Python OOP. I'm fairly new to OOP and still trying to get my head around the concepts.
我知道类是所有对象的蓝图.我喜欢将类视为一个实体,每个对象都是该实体的记录.我来自数据库背景,所以这就是为什么我这样解释它,随时可以纠正我.
I understand that a Class is a blueprint for all objects. I like to think of a Class as an entity and each Object is a record of that entity. I am from a Database background so that's why I interpret it like this, feel free to correct me.
无论如何,我正在制作的联系人"应用程序中创建了如下所示的"联系人
"类:
Anyways, in the Contacts app I'm making I've created the Class Contacts
as outlined below:
class Contacts():
def __init__(self, firstName, lastName, address, groupType,
telephone, mobile, email, photoField):
self.firstName = firstName
self.lastName = lastName
self.address = address
self.groupType = groupType
self.telephone = telephone
self.mobile = mobile
self.email = email
self.photoField = photoField
def showDetails(self):
print("First Name:\t", self.firstName)
print("Last Name:\t", self.lastName)
print("Address:\t", self.address)
print("Telephone:\t", self.telephone)
print("Mobile:\t", self.mobile)
print("Email:\t", self.email)
现在,如果我想通过Contacts类将联系人添加到我用来存储每个联系人对象的列表中,我是否必须创建一个 AddContacts
类,还是改为创建一个函数?我不知道我对问题的理解是否足够充分,以至于您无法理解我的意思?
Now, if I want to add contacts through the Contacts class into the list I'm using to store each contact object, do I have to create an AddContacts
class, or do I create a function instead? I don't know if I'm putting my question across well enough for you to understand what I mean?
我想我想说的是,添加联系人是一个过程,如果您从数据库的角度来看它,则不会创建名为"tbl_AddContacts"的表,因为您可以通过查询来完成此操作或存储过程,所以在我看来,我将定义添加联系人为函数.但是问我的同事谁进行C#编程时,他说添加联系人应该由Class定义.
What I guess I'm trying to say is that adding contacts is a process and if you look at it from a database point of view you wouldn't create a table called "tbl_AddContacts" since you would do that via a query or a stored procedure, so in my view I would define adding contacts being a function. But asking my colleague who does C# programming he says that adding contacts should be defined by a Class.
这对我来说是一个令人困惑的概念,尤其是因为我不了解如何将 AddContacts
类与 Contacts
类链接在一起,甚至不知道如何定义首先是 AddContacts
类!.
This is a confusing concept for me, especially since I don't understand how I would link the AddContacts
class with the Contacts
class, or even how to define an AddContacts
class in the first place!.
这是我定义的用于添加联系人的功能:
Here is the function I defined for adding contacts:
def addContacts():
firstName = input("First Name: ")
lastName = input("Last Name: ")
address = input("Address: ")
telephone = input("Telephone: ")
mobile = input("Mobile: ")
email = input("Email: ")
print("\n")
contact = Contacts(firstName, lastName, address, None, telephone, mobile, email, None)
contactsList.append(contact)
pickle.dump(contactsList, open("save.p", "wb"))
请帮助我,因为我会将其转换为GUI应用程序(单作业).
Please help me out, since I will turning this into a GUI application (uni assignment).
推荐答案
添加联系人是在做某事,而不是做某事,因此将其作为方法/函数而不是类是有意义的.我建议您的功能实际上应该放在两个单独的位置.
Adding a contact is doing something, rather than being something, so it would make sense as a method/function rather than a class. I would suggest that your functionality should actually be in two separate places.
根据用户输入创建新联系人应该是 Contact
的 class方法:
Creating a new contact from user input should be a class method of Contact
:
class Contact(object):
...
@classmethod
def from_input(cls):
firstName = input("First Name: ")
lastName = input("Last Name: ")
address = input("Address: ")
telephone = input("Telephone: ")
mobile = input("Mobile: ")
email = input("Email: ")
return cls(firstName, lastName, address, None,
telephone, mobile, email, None)
将新联系人添加到联系人列表应该是:
Adding a new contact to the list of contacts should either be:
- 例如的实例方法
AddressBook
或ContactList
类(或保存联系人列表的任何类);或 - 单独的功能(如果您没有用于容纳
Contact
实例的类).
- An instance method of the e.g.
AddressBook
orContactList
class (or whatever you have holding the contact list); or - A separate function if you don't have a class to hold the
Contact
instances.
例如:
class AddressBook(object):
...
def add_contact(self, contact=None):
if contact is None:
contact = Contact.from_input()
self.contacts.append(contact)
现在,您的UI可以创建一个 Contact
,并将其直接传递给 address_book.add_contact()
.
Now your UI can create a Contact
and pass it straight to address_book.add_contact()
.
这篇关于Python-何时创建类以及何时创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!