简体   繁体   English

在 OOP Python ZE5BA8B4C539C287BAAAEZ34

[英]What is the best way to use one function with same parameters in multiple classes in OOP Python tkinter

I'm trying to figure out how to make a function that will be compatible with two classes.我试图弄清楚如何制作与两个类兼容的 function。 I have two classes with exact same parameters as the other, plus some more on each.我有两个类的参数与另一个类完全相同,每个类都有更多参数。 And I need to use a function that is totally identical in both classes.而且我需要使用两个类中完全相同的 function。 Is there a way to use the function while only defying it once?有没有一种方法可以使用 function 而只挑战一次?

To make it more clear, here is my case.为了更清楚,这是我的情况。 I have a Tkinter app with the form .我有一个带有表单Tkinter应用程序。 The form is class by itself, but I also have a class that displays that from but filled and disabled by default as the display of details about a registered person .表单本身是 class ,但我也有一个 class ,它显示来自但默认填充和禁用的表单作为关于注册人的详细信息的显示。 After clicking a button, you can change data in that form and update it in the database.单击按钮后,您可以更改该表单中的数据并在数据库中更新它。 Because the two classes are so similar, I used naming and everything from the form class to build the person detail class.因为这两个类非常相似,所以我使用命名和 class 形式的所有内容来构建人员详细信息 class。 But here is where I want to call the validation function.但在这里我想调用验证 function。 The same function would be called when submitting and also updating data.提交和更新数据时将调用相同的 function。 In each class, it would take only the self parameter, but I don't know how to define it only once and use it inside both Classes在每个 class 中,它只需要self参数,但我不知道如何只定义一次并在两个类中使用它

in python as in many other programing languages you can create child classes which can inherit some attributes(parameters) and methods(functions) for parent class.在 python 和许多其他编程语言中一样,您可以创建子类,这些子类可以继承父 class 的一些属性(参数)和方法(函数)。 Best practice in Object Oriented Programing for situations where some classes share same elements is to create one parent class which is inherited by both your child classes and by doing that you can pass them different parameters but write code only once.对于某些类共享相同元素的情况,Object 面向编程的最佳实践是创建一个父 class,它由您的两个子类继承,通过这样做,您可以向它们传递不同的参数,但只编写一次代码。

class parent:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def shared_method(self, *some_parameters):
        # This method is doing something useful
        pass


# by putting some class name in brackets you are inheriting all from that class
class child1(parent):

    def __init__(self, first, second, third):
        # keyword super lets you avoid referring to the base class explicitly and takes for you everything that is already in parent's __init__
        super().__init__(first, second)
        self.third = third  # and you can than define new attributes specific to that class


class child2(parent):

    def __init__(self, first, second):
        super().__init__(first, second)

    def new(self, x):
        pass

Some advanced things you can research next are: how to create abstract classes so you can create objects only from child classes and not from parent and how to overwrite methods from parent class.您接下来可以研究的一些高级内容是:如何创建抽象类,以便您只能从子类而不是父类创建对象,以及如何覆盖父类 class 的方法。

Object Oriented Programing (OOP) is large field in programming, but I hope I helped you at least get started and know what to look for next. Object 面向编程(OOP)是编程中的一个大领域,但我希望我至少能帮助你入门并知道下一步该寻找什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将列表从 python function 导入 tkinter GUI 的最佳方法是什么 - What is the best way to Importing a list from a python function into tkinter GUI 在 Python 个类中初始化和使用常量的最佳方法是什么? - What's the best way to initialise and use constants across Python classes? 在python中将多个参数传递给init类的最佳方法是什么? - What is the best way to pass multiple parameters to class init in python? 在另一个 function (Python)中使用来自一个 function 的变量的最佳方法是什么? - What is the best way to use a variable from one function in another function (Python)? 在python中绘制具有多个参数的数学函数的最佳方法? - The best way to plot a math function with multiple parameters in python? 如何在一个类中实例化多个类:Python OOP - How to instantiate multiple classes in one class: Python OOP Python 中在不同线程中调用相同函数的最佳方法是什么? - What is the best way in Python to call the same function in separate threads? 在python中重复运行相同的java函数的最佳方法是什么? - What is the best way to run the same java function repeatedly in python? 调用类的方法以更改位置的最佳方法tkinter python类 - best way to call a method of a class to change location tkinter python classes 在 Python 库上启动参数的最佳方法是什么? - What is the best way to initiate parameters on a Python library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM