Learning Perl Objects, References amp;amp; Modules [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learning Perl Objects, References amp;amp; Modules [Electronic resources] - نسخه متنی

Randal L. Schwartz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید














9.5 Inheriting the Constructor


Was
there anything specific to Horse in that method?
No. Therefore, it's also the same recipe for
building anything else inherited from Animal, so
let's put it there:

{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "!\n"
}
sub name {
my $self = shift;
$$self;
}
sub named {
my $class = shift;
my $name = shift;
bless \$name, $class;
}
}
{ package Horse;
@ISA = qw(Animal);
sub sound { "neigh" }
}

Ahh, but what happens if you invoke speak on an
instance?

my $tv_horse = Horse->named("Mr. Ed");
$tv_horse->speak;

You get a debugging value:

a Horse=SCALAR(0xaca42ac) goes neigh!

Why? Because the
Animal::speak routine expects a classname as its
first parameter, not an instance. When the instance is passed in,
you'll use a blessed scalar reference as a string,
which shows up as you saw it just nowsimilar to a stringified
reference, but with the class name in front.



/ 199