பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்): திருத்தங்களுக்கு இடையிலான வேறுபாடு
உள்ளடக்கம் நீக்கப்பட்டது உள்ளடக்கம் சேர்க்கப்பட்டது
சிNo edit summary |
வார்ப்புரு |
||
வரிசை 1:
{{mergeto|பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்)}}
பொருள் நோக்கு நிரலாக்கத்தில், '''பல்லுருவாக்கம்''' (Polymorphism) என்பது ஒரு வகுப்பின் செயலிகளை, மாறிகளை, அல்லது பொருட்களை அந்த வகுப்பின் subclasses தமது தேவைகளுக்கு ஏற்றமாதிரி நிறைவேற்ற முடியும் என்ற கூற்றாகும்.
== எடுத்துக்காட்டுக்கள் ==
=== பி.எச்.பி ===
<source lang=php>
<?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();
}
?>
</source>
=== பெர்ள் ===
Polymorphism in [[Perl]] is inherently straightforward to write because of the language's use of [[Sigil (computer programming)|sigils]] and [[Reference (computer science)|references]]. This is the <code>Animal</code> example in standard OO Perl:
<source lang=perl>
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!
</source>
This means that Perl can also apply polymorphism to the method call. The example below is written using the <code>Moose</code> module in order to show modern OO practises in Perl (it is not required for method polymorphism):
<source lang=perl>
{
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
</source>
== வெளி இணைப்புகள் ==
* [http://www.devshed.com/c/a/PHP/PHP-5-and-Polymorphism/ PHP 5 and Polymorphism]
[[பகுப்பு:பொருள் நோக்கு நிரலாக்கம்]]
|