Python 使用其他类的方法

发布于 2024-08-31 20:06:45 字数 55 浏览 4 评论 0原文

如果我有两个类,其中一个类有一个我想在另一个类中使用的函数,我应该使用什么才能不必重写我的函数?

If I have two classes, and one of them has a function that I want to use in my other class, what do I use so that I don't have to rewrite my function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-09-07 20:06:45

有两种选择:

示例:

class A(object):
    def a1(self):
        """ This is an instance method. """
        print "Hello from an instance of A"

    @classmethod
    def a2(cls):
        """ This a classmethod. """
        print "Hello from class A"

class B(object):
    def b1(self):
        print A().a1() # => prints 'Hello from an instance of A'
        print A.a2() # => 'Hello from class A'

或者使用继承(如果合适):

class A(object):
    def a1(self):
        print "Hello from Superclass"

class B(A):
    pass

B().a1() # => prints 'Hello from Superclass'

There are two options:

  • instanciate an object in your class, then call the desired method on it
  • use @classmethod to turn a function into a class method

Example:

class A(object):
    def a1(self):
        """ This is an instance method. """
        print "Hello from an instance of A"

    @classmethod
    def a2(cls):
        """ This a classmethod. """
        print "Hello from class A"

class B(object):
    def b1(self):
        print A().a1() # => prints 'Hello from an instance of A'
        print A.a2() # => 'Hello from class A'

Or use inheritance, if appropriate:

class A(object):
    def a1(self):
        print "Hello from Superclass"

class B(A):
    pass

B().a1() # => prints 'Hello from Superclass'
金橙橙 2024-09-07 20:06:45

有几种方法:

  • 继承
  • 委托
  • 超级偷偷摸摸的委托

以下示例使用每种方法来共享打印成员的函数。

继承

class Common(object):
    def __init__(self,x):
        self.x = x
    def sharedMethod(self):
        print self.x

class Alpha(Common):
    def __init__(self):
        Common.__init__(self,"Alpha")

class Bravo(Common):
    def __init__(self):
        Common.__init__(self,"Bravo")

委托

class Common(object):
    def __init__(self,x):
        self.x = x
    def sharedMethod(self):
        print self.x

class Alpha(object):
    def __init__(self):
         self.common = Common("Alpha")
    def sharedMethod(self):
         self.common.sharedMethod()

class Bravo(object):
    def __init__(self):
         self.common = Common("Bravo")
    def sharedMethod(self):
         self.common.sharedMethod()

超级偷偷摸摸的委托
该解决方案基于这样一个事实:Python 成员函数没有什么特别之处;您可以使用任何函数或可调用对象,只要第一个参数被解释为类的实例即可。

def commonPrint(self):
    print self.x

class Alpha(object):
    def __init__(self):
        self.x = "Alpha"
    sharedMethod = commonPrint

class Bravo(object):
    def __init__(self):
        self.x = "Bravo"
    sharedMethod = commonPrint

或者,实现委托的一种类似的偷偷摸摸的方法是使用可调用对象:

class Printable(object):
   def __init__(self,x):
       self.x = x
   def __call__(self):
       print self.x

class Alpha(object):
   def __init__(self):
       self.sharedMethod = Printable("Alpha")

class Bravo(object):
   def __init__(self):
       self.sharedMethod = Printable("Bravo")

There are several approaches:

  • Inheritance
  • Delegation
  • Super-sneaky delegation

The following examples use each for sharing a function that prints a member.

Inheritance

class Common(object):
    def __init__(self,x):
        self.x = x
    def sharedMethod(self):
        print self.x

class Alpha(Common):
    def __init__(self):
        Common.__init__(self,"Alpha")

class Bravo(Common):
    def __init__(self):
        Common.__init__(self,"Bravo")

Delegation

class Common(object):
    def __init__(self,x):
        self.x = x
    def sharedMethod(self):
        print self.x

class Alpha(object):
    def __init__(self):
         self.common = Common("Alpha")
    def sharedMethod(self):
         self.common.sharedMethod()

class Bravo(object):
    def __init__(self):
         self.common = Common("Bravo")
    def sharedMethod(self):
         self.common.sharedMethod()

Super-sneaky Delegation
This solution is based off of the fact that there is nothing special about Python member functions; you can use any function or callable object so long as the first parameter is interpreted as the instance of the class.

def commonPrint(self):
    print self.x

class Alpha(object):
    def __init__(self):
        self.x = "Alpha"
    sharedMethod = commonPrint

class Bravo(object):
    def __init__(self):
        self.x = "Bravo"
    sharedMethod = commonPrint

Or, a similarly sneaky way of achieving delegation is to use a callable object:

class Printable(object):
   def __init__(self,x):
       self.x = x
   def __call__(self):
       print self.x

class Alpha(object):
   def __init__(self):
       self.sharedMethod = Printable("Alpha")

class Bravo(object):
   def __init__(self):
       self.sharedMethod = Printable("Bravo")
套路撩心 2024-09-07 20:06:45

您创建一个类,两个类都继承自该类。

存在多重继承,因此如果它们已经有父级,那么这不是问题。

class master ():
    def stuff (self):
        pass

class first (master):
    pass


class second (master):
    pass


ichi=first()
ni=second()

ichi.stuff()
ni.stuff()

you create a class from which both classes inherit.

There is multiple inheritance, so if they already have a parent it's not a problem.

class master ():
    def stuff (self):
        pass

class first (master):
    pass


class second (master):
    pass


ichi=first()
ni=second()

ichi.stuff()
ni.stuff()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文