我正在尝试使用GObject Introspection和Moo在 Perl 中创建 Gtk3 应用程序。有一个来自 Gtk 的非 Moo 类Gtk::ApplicationWindow
,我通过 Moo 使用extends 'Gtk::ApplicationWindow'
. 问题是,当创建该子类的对象时,它仍然是父类的类型 - 即Gtk::ApplicationWindow
.
我通过子类化我自己的非 Moo 类来尝试同样的事情,并且从这个子类创建的对象是正确的类型。造成这种差异的原因可能是什么?
use v5.10;
use strict;
use warnings;
# Import the Gtk classes (non-Moo)
use Glib::Object::Introspection;
Glib::Object::Introspection->setup(basename => 'Gtk', version => '3.0', package => 'Gtk');
Glib::Object::Introspection->setup(basename => 'Gio', version => '2.0', package => 'Gio');
#################################################
{
# A dummy non-Moo class
package ClassNonMoo;
sub new { bless {}, shift; }
}
{
# Moo class extending the dummy class
package ClassMoo;
use Moo;
extends 'ClassNonMoo';
sub FOREIGNBUILDARGS {
my ($class, %args) = @_;
return ($args{app});
}
}
#################################################
{
# Moo class extending Gtk::ApplicationWindow
package ClassMooGtkAppWin;
use Moo;
extends 'Gtk::ApplicationWindow';
sub FOREIGNBUILDARGS {
my ($class, %args) = @_;
return ($args{app});
}
}
#################################################
# Create objects of ClassMoo and ClassMooGtkAppWin
sub create_objects {
my ($app) = @_;
my $o1 = ClassMoo->new( app => $app );
my $o2 = ClassMooGtkAppWin->new( app => $app );
say "o1 = $o1\no2 = $o2";
# Output:
# o1 = ClassMoo=HASH(0x2f7bc50)
# o2 = Gtk::ApplicationWindow=HASH(0x2f7bd40)
#
# Shouldn't o2 be of the type ClassMooGtkAppWin ?
exit(0);
}
# We can create a GtkApplicationWindow only after creating a GtkApplication and
# running it. This code just ensures that create_object() is called once the
# application is 'active'.
my $app = Gtk::Application->new('org.test', 'flags-none');
$app->signal_connect(activate => sub { create_objects($app) });
$app->run();