关于 FXRuby 中 ruby 继承、ruby 对象的非常菜鸟问题
首先为我糟糕的英语感到抱歉...我有一个疑问..我正在为务实的程序员阅读FXRuby..我看到了这段代码
require 'fox16'
include Fox
class HelloWindow < FXMainWindow
def initialize(app)
super(app, "Hello, World!" , :width => 200, :height => 100)
end
def create
super
show(PLACEMENT_SCREEN)
end
end
app = FXApp.new
HelloWindow.new(app)
app.create
app.run
这是一个非常基本的例子...实际上这是他的第一个例子..但我我太菜鸟了,我不明白:
app 是一个 FXAPP 对象.. 现在我创建一个 HelloWindow 对象并传递名为“app”的 FXApp 对象,
到目前为止一切顺利
,但现在......在书中写 app.create 我在 FXApp 类中调用“create”方法...或者不是?..
为什么当我调用 app.create..ruby 时调用 HelloWindow 中的创建方法?..app 是一个与 HelloWindow 类非常不同的对象,我可以调用祖先方法(就像我使用 super 时一样),但不能以相反的方式调用...
为什么他们不这样调用它这样
helloobject=HelloWindow.new(app)
helloobject.create
我在 HelloWindows 类中调用 create 方法..它是 FXMainWindows 的后代
我希望你能理解(抱歉我的英语不好)并且可以帮助我
非常感谢
first sorry for my poor english...I've a doubt..I'm reading the FXRuby for the pragmatic programmer..and I saw this code
require 'fox16'
include Fox
class HelloWindow < FXMainWindow
def initialize(app)
super(app, "Hello, World!" , :width => 200, :height => 100)
end
def create
super
show(PLACEMENT_SCREEN)
end
end
app = FXApp.new
HelloWindow.new(app)
app.create
app.run
It's a very basic example...actually It's he first example..but I'm so noob than I don't understand it:
app is a FXAPP object..
now I create a HelloWindow object and pass my FXApp object named "app"
so far so good
but now...in the book write app.create
I'm calling the "create" method inside FXApp class...or not?..
why when I call app.create..ruby call the create method inside HelloWindow?..app is a very different object than HelloWindow class and I could can call an anscestor method (like when I use super) but not at the inverse way...
why they don't call it something like this
helloobject=HelloWindow.new(app)
helloobject.create
this way I calling the create method inside HelloWindows class..and it is descendent from FXMainWindows
I hope than you can understand (sorry for my bad english) and can help me
thanks so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 FXRuby 一无所知,但我会回答您有关 Ruby 方面的问题。
当Ruby执行
app.create
时,它会调用FXApp类内部的create
方法,因为app
的类型是FXApp(假设有没有为单例应用程序类定义create
方法)。当您调用
app.create
时,FXApp 类中可能有一些代码在应用程序中的所有窗口上调用create
,因此您的窗口的方式如下: create
函数被调用。如果您想真正了解窗口的create
函数是如何被调用的,请尝试向其中添加raise "hello"
并查看是否获得异常的回溯。我真的不知道你最后一个问题的答案,因为它与 FXRuby 库的设计有关。但从概念上讲,调用
app.create
和window.create
似乎是非常不同的事情。如果您想运行该应用程序,您应该首先创建它。仅仅创建一个窗口是不够的。I don't know anything about FXRuby, but I answer your questions about the Ruby side of things.
When Ruby executes
app.create
, it will call thecreate
method inside the FXApp class becauseapp
's type is FXApp (assuming that there is nocreate
method defined for the singleton class of app).When you call
app.create
, there is probably some code in the FXApp class to callscreate
on all of the windows in the app, so that's how your window'screate
function gets called. If you want to really find out how your window'screate
function is being called, try addingraise "hello"
to it and see if you get a backtrace of the exception.I don't really know the answer to your last question because it has to do with the design of the FXRuby library. But conceptually it seems like calling
app.create
andwindow.create
are very different things. If you want to run the app, you should create it first. Simply creating one window isn't good enough.