开发者

Do I need to use a class to use its methods in my subclass in Perl?

Alrighty, coding in Perl and just had a quick question. I have class created called SubtitleSite which is basically an abstraction, and a class called podnapisi that inherits SubtitleSite like this:

@ISA = qw(SubtitleSite);

My question is, do I have to use:

use SubtitleSite;

in order to have access to all 开发者_如何学Gothe methods in SubtitleSite?


Yes, but most of the time you're better off not messing with @ISA directly. Just use parent qw(SubtitlesSite);, it will load SubtilteSite for you and add it to @ISA.


Yes.
Some more info can be found here:

  • http://www.troubleshooters.com/codecorn/littperl/perloop.htm#PerlInheritance


YES.

Otherwise the symbols defined in SubtitleSite are undefined in podnapisi.


In order to access the methods, you'd either have to inherit from it, or delegate to an object of it's type.


If you are constructing an object in your child class, you can just call methods on yourself and they will be found through the magic of inheritance (see perldoc perlobj for more about SUPER):

sub foo
{
    my $this = shift;
    $this->method_on_parent;  # this works!
    $this->SUPER::foo;        # this works too
}

However if these classes are only library functions that don't use OO, you have to tell Perl explicitly where to find the function:

ParentClass::function;    # use the class name explicitly
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜