பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்)
பொருள் நோக்கு நிரலாக்கத்தில், பல்லுருவாக்கம் (Polymorphism) என்பது ஒரு வகுப்பின் செயலிகளை, மாறிகளை, அல்லது பொருட்களை அந்த வகுப்பின் subclasses தமது தேவைகளுக்கு ஏற்றமாதிரி நிறைவேற்ற முடியும் என்ற கூற்றாகும். எடுத்துக்காட்டுக்கள்பி.எச்.பி<?php
interface IAnimal
{
function getName();
function talk();
}
abstract class AnimalBase implements IAnimal
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
class Cat extends AnimalBase
{
public function talk()
{
return "Meowww!";
}
}
class Dog extends AnimalBase
{
public function talk()
{
return "Arf! Arf!";
}
}
$animals = array(
new Cat("Missy"),
new Cat("Mr. Mistoffelees"),
new Dog("Lassie")
);
foreach ($animals as $animal) {
echo $animal->getName() . ": " . $animal->talk();
}
?>
பெர்ள்Polymorphism in பெர்ள் is inherently straightforward to write because of the language's use of sigils and references. This is the package Animal;
sub new {
my ($class, $name) = @_;
bless {name => $name}, $class;
}
package Cat;
@ISA = "Animal";
sub talk {"Meow"}
package Dog;
@ISA = "Animal";
sub talk {"Woof! Woof!"}
package main;
my @animals = (
Cat->new("Missy"),
Cat->new("Mr. Mistoffelees"),
Dog->new("Lassie"),
);
for my $animal (@animals) {
print $animal->{name} . ": " . $animal->talk . "\n";
}
# prints the following:
#
# Missy: Meow
# Mr. Mistoffelees: Meow
# Lassie: Woof! Woof!
This means that Perl can also apply polymorphism to the method call. The example below is written using the {
package Animal;
use Moose;
has 'name' => ( isa => 'Str', is => 'ro' );
}
{
package Cat;
use Moose;
extends 'Animal';
sub talk { 'Meow' }
sub likes { 'Milk' }
}
{
package Dog;
use Moose;
extends 'Animal';
sub talk { 'Woof! Woof!' }
sub likes { 'Bone' }
}
my @animals = (
Cat->new( name => 'Missy' ),
Cat->new( name => 'Mr. Mistoffelees' ),
Dog->new( name => 'Lassie' ),
);
for my $animal ( @animals ) {
for my $trait qw/talk likes/ {
print $animal->name . ': ' . $trait . ' => ' . $animal->$trait;
}
}
# prints the following:
#
# Missy: talk => Meow
# Missy: likes => Milk
# Mr. Mistoffelees: talk => Meow
# Mr. Mistoffelees: likes => Milk
# Lassie: talk => Woof! Woof!
# Lassie: likes => Bone
வெளி இணைப்புகள்
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia