先发制人的OOP? [英] preemptive OOP?

查看:78
本文介绍了先发制人的OOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我今天有一个新的随机问题 - 随意忽略和

回到你真正的工作! :)


无论如何,我正在创建一个GUI(是的,我的主计划的所有部分都是为了
最终有某种数据库应用程序工作)并且它将涉及一个wx.Notebook控件。
。我想我有两个选择

我怎么能这样做。在主窗口框架的类中,我可以说:


notebook = wx.Notebook(面板)#panel是Notebook控件的父级


这使用默认的wx.Notebook类,并且工作得很好。但是我想是这样做的想法,这样做是一个明智的想法:


class MyNotebook(wx.Notebook):

def __init __(self,parent):

wx.Notebook .__ init __(self,parent)


然后从主框架调用它: br />

notebook = MyNotebook(面板)


这似乎允许未来扩展定制笔记本

类,但与此同时我不知道我是怎么想或为什么要这样做的。


所以我的问题一般是,默认为OOP是个好主意

设计就像我的第二个例子,当你甚至不确定你需要

吗?我知道它不会受伤,有时可能很聪明,但

也许它只是为程序添加了不必要的代码。

解决方案


class MyNotebook(wx.Notebook):

def __init __(self,parent):

wx.Notebook .__ init __(self,parent)


所以我的问题一般是,默认为OOP是一个好主意

设计就像我的第二个例子,当你甚至不确定你需要

吗?我知道它不会受伤,有时可能很聪明,但

也许它只会为程序添加不必要的代码。



我的感觉是,如果他们没有提供额外的数据或行为,没有充分的理由增加你自己的定制类的复杂性。如果你知道你有计划将你自己的属性和/或方法很快添加到MyNotebook

,那么现在这样做是没有害处的,但为什么呢?你总是可以通过多态的mircale来创建你自己的自定义子类,当你有一些有价值的东西时,它可以在任何地方运行一个笔记本
/>
会。


-ej


Erik Johnson写道:


我的感觉是,如果他们没有提供额外的数据或行为,就没有充分的理由来增加你自己的自定义类的复杂性。如果你知道你有计划将你自己的属性和/或方法很快添加到MyNotebook

,那么现在这样做是没有害处的,但为什么呢?你总是可以通过多态的mircale来创建你自己的自定义子类,当你有一些有价值的东西时,它可以在任何地方运行一个笔记本
/>
会。



我认为你是对的。事实上,我一直喜欢Python的一件事就是

OOP是可选的,在这里我试图强迫它自己

不必要! :)


John Salerno写道:


Erik Johnson写道:

< blockquote class =post_quotes>
>我的感觉是,如果没有提供额外的数据或行为,就没有充分的理由来增加自定义类的复杂性。如果您知道您计划尽快将自己的属性和/或方法添加到MyNotebook中,那么现在这样做是没有害处的,但为什么呢?你可以随时创建你自己的自定义子类,当你有一些有价值的东西要通过多态的mircale来添加它时,它会在笔记本的任何地方运行。




我认为你是对的。事实上,我一直喜欢Python的一件事就是

OOP是可选的,在这里我试图强迫它自己

不必要! :)



但是,如果你想简化代码,你可以先说


myNotebook = wx.Notebook


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http:// del。 icio.us/steve.holden


Ok, I have a new random question for today -- feel free to ignore and
get back to your real jobs! :)

Anyway, I''m creating a GUI (yes, all part of my master plan to
eventually have some sort of database application working) and it''s
going to involve a wx.Notebook control. I think I have two options for
how I can do this. Within the class for the main window frame, I can say:

notebook = wx.Notebook(panel) # panel is parent of the Notebook control

This uses the default wx.Notebook class, and works just fine. But I was
thinking, is it a smart idea to do this instead:

class MyNotebook(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent)

and then call it from the main frame as:

notebook = MyNotebook(panel)

This seems to allow for future expansion of the customized Notebook
class, but at the same time I have no idea how or why I''d want to do that.

So my question in general is, is it a good idea to default to an OOP
design like my second example when you aren''t even sure you will need
it? I know it won''t hurt, and is probably smart to do sometimes, but
maybe it also just adds unnecessary code to the program.

解决方案

class MyNotebook(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent)

So my question in general is, is it a good idea to default to an OOP
design like my second example when you aren''t even sure you will need
it? I know it won''t hurt, and is probably smart to do sometimes, but
maybe it also just adds unnecessary code to the program.

My feeling is that there is no good reason to add the complexity of your
own custom classes if they provide no additional data or behaviour. If you
know you have plans to add your own attributes and/or methods to MyNotebook
soon, there''s no harm in doing this now, but why? You can always create your
own custom subclass at the point when you have something of value to add and
through the mircale of polymorphism, it will function everywhere a Notebook
would.

-ej


Erik Johnson wrote:

My feeling is that there is no good reason to add the complexity of your
own custom classes if they provide no additional data or behaviour. If you
know you have plans to add your own attributes and/or methods to MyNotebook
soon, there''s no harm in doing this now, but why? You can always create your
own custom subclass at the point when you have something of value to add and
through the mircale of polymorphism, it will function everywhere a Notebook
would.

I think you''re right. In fact, one thing I always loved about Python was
that OOP is optional, and here I am trying to force it upon myself
unnecessarily! :)


John Salerno wrote:

Erik Johnson wrote:

> My feeling is that there is no good reason to add the complexity of your
own custom classes if they provide no additional data or behaviour. If you
know you have plans to add your own attributes and/or methods to MyNotebook
soon, there''s no harm in doing this now, but why? You can always create your
own custom subclass at the point when you have something of value to add and
through the mircale of polymorphism, it will function everywhere a Notebook
would.



I think you''re right. In fact, one thing I always loved about Python was
that OOP is optional, and here I am trying to force it upon myself
unnecessarily! :)

However, if you want to simplify the code, you could start by saying

myNotebook = wx.Notebook

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


这篇关于先发制人的OOP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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