如何在Perl中的基类和子类之间共享变量? [英] How can I share variables between a base class and subclass in Perl?
问题描述
我有一个这样的基类:
package MyClass;
use vars qw/$ME list of vars/;
use Exporter;
@ISA = qw/Exporter/;
@EXPORT_OK = qw/ many variables & functions/;
%EXPORT_TAGS = (all => \@EXPORT_OK );
sub my_method {
}
sub other_methods etc {
}
--- more code---
我想对 MyClass
进行子类化
package MySubclass;
use MyClass;
use vars qw/@ISA/;
@ISA = 'MyClass';
sub my_method {
--- new method
}
我想将其称为 MySubclass
,就像我原来的 MyClass
,并且仍然可以从导出器
访问所有变量和函数。但是,我无法从原始类 MyClass
正确导出 Exporter
变量。我是否需要在子类中再次运行 Exporter
?
And I want to call this MySubclass
like I would the original MyClass
, and still have access to all of the variables and functions from Exporter
. However I am having problems getting the Exporter
variables from the original class, MyClass
, to export correctly. Do I need to run Exporter
again inside the subclass? That seems redundant and unclear.
示例文件:
#!/usr/bin/perl
use MySubclass qw/$ME/;
-- rest of code
但是尝试时出现编译错误导入 $ ME
变量。有建议吗?
But I get compile errors when I try to import the $ME
variable. Any suggestions?
推荐答案
您实际上并没有从<<继承$ MySubclass
code> MyClass 完全- MySubClass
是 MyClass <的用户/ code>。您正在执行的操作是覆盖
MyClass
的行为,但是如果您将其视为继承,则只会混淆自己,因为它不是(例如:您的构造函数在哪里?)我无法弄清楚您要做什么,直到我忽略了代码正在做的所有事情,而只是阅读了您对要发生的事情的描述。
You're not actually inheriting MySubclass
from MyClass
at all -- MySubClass
is a user of MyClass
. What you're doing is overriding a bit of behaviour from MyClass
, but you will only confuse yourself if you think of this as inheritance, because it isn't (for example: where is your constructor?) I couldn't figure out what you were trying to do until I ignored everything the code was doing and just read your description of what you want to have happen.
好的,所以您有一个导入一些符号的类-一些函数和一些变量:
Okay, so you have a class which imports some symbols - some functions, and some variables:
package MyClass;
use strict;
use warnings;
use Exporter 'import'; # gives you Exporter's import() method directly
our @EXPORT_OK = qw/ many variables & functions/;
our %EXPORT_TAGS = (all => \@EXPORT_OK );
our ($ME, $list, $of, $vars);
sub my_func {
}
sub other_func {
}
1;
然后您来编写一个从MyClass导入一切的类,然后再次全部导入,但将一个函数换成另一个:
and then you come along and write a class which imports everything from MyClass, imports it all back out again, but swaps out one function for another one:
package MyBetterclass;
use strict;
use warnings;
use Exporter 'import'; # gives you Exporter's import() method directly
our @EXPORT_OK = qw/ many variables & functions /;
our %EXPORT_TAGS = (all => \@EXPORT_OK );
use MyClass ':all';
sub my_func {
# new definition
}
1;
就是这样!请注意,我启用了严格的检查和警告,并更改了实际起作用的方法的名称。
此外,我没有使用 use vars
(文档说它已经过时了,所以如果您仍然想在不了解其机制的情况下使用它,那将是一个很大的警告。)
That's it! Note that I enabled strict checking and warnings, and changed the names of the "methods" that are actually functions.
Additionally, I did not use use vars
(the documentation says it's obsolete, so that's a big red flag if you still want to use it without understanding its mechanics).
这篇关于如何在Perl中的基类和子类之间共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!