[Perl]繼承

parent

package Baz;
use parent qw(Foo Bar);

is equivalent to:

package Baz;
BEGIN {
  require Foo;
  require Bar;
  push @ISA, qw(Foo Bar);
}

-norequire

package Foo;
sub exclaim { "I CAN HAS PERL" }
package DoesNotLoadFooBar;
use parent -norequire, 'Foo', 'Bar';
# will not go looking for Foo.pm or Bar.pm

is equivalent to

package Foo;
sub exclaim { "I CAN HAS PERL" }
package DoesNotLoadFooBar;
push @DoesNotLoadFooBar::ISA, 'Foo', 'Bar';

This is also helpful for the case where a package lives within a differently named file:

package MyHash;
use Tie::Hash;
use parent -norequire, 'Tie::StdHash';

#This is equivalent to the following code:
 
package MyHash;
require Tie::Hash;
push @ISA, 'Tie::StdHash';

If you want to load a subclass from a file that require would not consider an eligible filename (that is, it does not end in either .pm or .pmc), use the following code:

package MySecondPlugin;
require './plugins/custom.plugin'; # contains Plugin::Custom
use parent -norequire, 'Plugin::Custom';

SUPER

By default, every base class needs to live in a file of its own. If you want to have a subclass and its parent class in the same file, you can tell parent not to load any modules by using the -norequire switch:

package A;
sub new {
  return bless {}, shift;
}
sub speak {
  my $self = shift;
  say 'A';
}

package B;
use parent -norequire, 'A';
sub speak {
  my $self = shift;
  $self->SUPER::speak();
  say 'B';
}

package C;
use parent -norequire, 'B';
sub speak {
  my $self = shift;
  $self->SUPER::speak();
  say 'C';
}

my $c = C->new();
$c->speak();
#---------
In this example, we will get the following output:
A
B
C

This demonstrates how SUPER is resolved. Even though the object is blessed into the C class, the speak() method in the B class can still call SUPER::speak() and expect it to correctly look in the parent class of B (i.e the class the method call is in), not in the parent class of C (i.e. the class the object belongs to).
There are rare cases where this package-based resolution can be a problem. If you copy a subroutine from one package to another, SUPER resolution will be done based on the original package.

Summay

  1. A subclass and its parent class can be in the same file.
  2. use parent -norequire, qw (parent) avoid compiling, which causes syntax error in scenario 1.
  3. SUPER:: calls parent 's method of current object
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末仅仆,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仔戈,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)靶擦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)雇毫,“玉大人玄捕,你說(shuō)我怎么就攤上這事∨锓牛” “怎么了枚粘?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)飘蚯。 經(jīng)常有香客問我馍迄,道長(zhǎng),這世上最難降的妖魔是什么局骤? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任攀圈,我火速辦了婚禮,結(jié)果婚禮上峦甩,老公的妹妹穿的比我還像新娘赘来。我一直安慰自己,他們只是感情好凯傲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布犬辰。 她就那樣靜靜地躺著,像睡著了一般冰单。 火紅的嫁衣襯著肌膚如雪忧风。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天球凰,我揣著相機(jī)與錄音狮腿,去河邊找鬼腿宰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛缘厢,可吹牛的內(nèi)容都是我干的吃度。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼贴硫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼椿每!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起英遭,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤间护,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后挖诸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體汁尺,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年多律,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了痴突。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡狼荞,死狀恐怖辽装,靈堂內(nèi)的尸體忽然破棺而出扛点,到底是詐尸還是另有隱情雕沉,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布植影,位于F島的核電站丰涉,受9級(jí)特大地震影響殷勘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜昔搂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望输拇。 院中可真熱鬧摘符,春花似錦、人聲如沸策吠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)猴抹。三九已至带族,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蟀给,已是汗流浹背蝙砌。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工阳堕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人择克。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓恬总,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親肚邢。 傳聞我的和親對(duì)象是個(gè)殘疾皇子壹堰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,497評(píng)論 0 23
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,864評(píng)論 0 6
  • 看不見的眼淚 流下是為誰(shuí) 看得見的眼淚 流下是心碎 我為我感到不值 為何淚在流 而無(wú)人安慰 流下是為誰(shuí) 我覺得憔悴...
    雪際陸痕閱讀 233評(píng)論 0 2
  • 收拾雜物,偶然發(fā)現(xiàn)一個(gè)收納箱里有七八個(gè)筆記本骡湖。打開一看贱纠,原來(lái)都是我工作后寫的日記。有的是用藍(lán)色墨水寫的响蕴,都...
    艾亦蘇閱讀 218評(píng)論 0 0
  • 比起下午的蛋糕谆焊,第二幅似乎更流暢,長(zhǎng)線條還是有待練習(xí)换途!
    小樣兒依舊閱讀 149評(píng)論 0 1