python类层次结构问题 [英] Issue with python class hierarchy

查看:100
本文介绍了python类层次结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类层次结构:

class ParentClass:

    def do_something(self):
        pass # child classes have their own implementation of this

class ChildClass1(ParentClass):

    def do_something(self):
        <implementation here>

class ChildClass2(ParentClass):

    def do_something(self, argument_x):
        <implementation here>

class ChildClass3(ParentClass):

    def do_something(self, argument_y):
        <implementation here>

这里有两个问题:

  • 方法do_something()在子类中具有不同的接口:它在子类2和3中接受一个参数,但在子类1中没有参数
  • do_something()的参数具有不同的名称,以强调它们在子类2和3中的含义不同.
  • method do_something() has different interfaces in subclasses: it accepts an argument in child classes 2 and 3, but has no argument in child class 1
  • the arguments of do_something() have different names to emphasize that they have different meanings in child classes 2 and 3. This will become clearer below, from the usage example

这是使用类的方式:

有一个工厂类返回实例:

There is a factory class that returns instances:

class ChildFactory:

    def get_child(self, argument):
        if argument == '1':
            return ChildClass1()
        elif argument == '2':
            return ChildClass2()
        elif argument == '3':
            return ChildClass3()

稍后的代码:

...
# pseudocode, not python
child_type = ? # can have values '1', '2' or '3' at this moment
var1 = 1
var2 = 'xxx'
# var1 and var2 have different types, just to emphasize the difference in their
# meaning when being passed as arguments to do_something()
# this was mentioned above (the second problem)
child = ChildFactory.get_child(child_type)
if child is an instance of ChildClass1, child.do_something() is called
if child is an instance of ChildClass2, child.do_something(var1) is called
if child is an instance of ChildClass3, child.do_something(var2) is called
# end of pseudocode

问题:

  1. 上面提到的两个问题是否表明设计不好?如果是这样,设计层次结构的正确方法是什么?
  2. 如何用python统一编写伪代码段?主要关注点是避免对每个特殊情况使用庞大的if/else语句,因为它将使ChildFactory.get_child()的if/else语句加倍.

推荐答案

具有相同名称和不同参数的方法是代码的味道.

Methods with the same name and different arguments are a code smell.

方法do_something()在子类中具有不同的接口:它在子类2和3中接受参数,但在子类1中不具有参数"

您没有说为什么.

  • 子类1具有默认值.

  • child class 1 has a default value.

子类2忽略该值.

几乎所有其他原因都表明do_something确实是 不同,并且应该使用不同的名称.

Almost any other reason indicates that the do_something is truly different and should have a different name.

如果子类1具有默认值,则只需在方法函数的参数中显式地编写默认值即可.

If child class 1 has a default value, then simply code a default value explicitly in the arguments to the method function.

class ChildClass1( ParentClass ):
    def do_something( argument_x= None )
        ....

如果子类1忽略该值,则只需忽略该值.不要站在你的头上以忽略vlaue.

If child class 1 ignores the value, then simply ignore the value. Don't stand on your head to ignore a vlaue.

class ChildClass1( ParentClass ):
    def do_something( argument_x )
        return True

一个多态方法函数并不能完全使用所有参数值,这是没有魔力的.

There's no magic about a polymorphic method function that doesn't happen to use all argument values.

"do_something()的参数具有不同的名称,以强调它们在子类2和3中的含义不同."

这只是糟糕的设计.不能使用具有不同参数名称的相同方法函数,因为它们执行不同的操作.

This is just bad design. You can't have the same method function with different argument names because they do different things.

具有相同的方法函数名称是错误的.如果它们是类似事物的不同实现,则参数将具有基本上相同的含义.

Having the same method function name is just wrong. If they are different implementations of similar things, then the arguments will have essentially the same meanings.

如果它们实际上做的是不同的事情,那么您就没有多态性,因此您不应该为这些方法使用相同的名称.

If they actually do different things, then you don't have polymorphism, and you should not be giving these methods the same names.

当两个类中的方法做的事情根本不同时-要求使用不同名称的不同参数来使其变得明显-这些方法不能具有相同的名称.当该名称没有描述该方法的实际作用时,它就不再具有意义.

When the methods in two classes do fundamentally different things -- requiring different arguments with distinct names to make that obvious -- those methods must not have the same names. The name ceases to have meaning when it doesn't describe what the method actually does.

注意

由于鸭子输入,您的代码BTW将在Python中工作.只要方法名称匹配,则参数类型甚至不必接近匹配.但是,这真的是一个糟糕的设计,因为这些方法之间的本质差异是如此之大.

Your code, BTW, will work in Python because of duck typing. As long as the method names match, the argument types do not have to even come close to matching. However, this is really poor design because the essential differences among the methods are so huge.

这篇关于python类层次结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆