பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்): திருத்தங்களுக்கு இடையிலான வேறுபாடு

கட்டற்ற கலைக்களஞ்சியமான விக்கிப்பீடியாவில் இருந்து.
உள்ளடக்கம் நீக்கப்பட்டது உள்ளடக்கம் சேர்க்கப்பட்டது
சி AntanO பக்கம் பல்லுருவாக்கம் (பொருள் நோக்கு நிரலாக்கம்)-ஐ [[பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்...
சிNo edit summary
வரிசை 1: வரிசை 1:
பொருள் நோக்கு நிரலாக்கத்தில், '''பல்லுருவாக்கம்''' (''Polymorphism'') என்பது ஒரு வகுப்பின் செயலிகளை, மாறிகளை, அல்லது பொருட்களை அந்த வகுப்பின் subclasses தமது தேவைகளுக்கு ஏற்றமாதிரி நிறைவேற்ற முடியும் என்ற கூற்றாகும்.
{{mergeto|பல்லுருத்தோற்றம் (கணிப்பொறி அறிவியல்)}}
பொருள் நோக்கு நிரலாக்கத்தில், '''பல்லுருவாக்கம்''' (Polymorphism) என்பது ஒரு வகுப்பின் செயலிகளை, மாறிகளை, அல்லது பொருட்களை அந்த வகுப்பின் subclasses தமது தேவைகளுக்கு ஏற்றமாதிரி நிறைவேற்ற முடியும் என்ற கூற்றாகும்.


== எடுத்துக்காட்டுக்கள் ==
== எடுத்துக்காட்டுக்கள் ==
=== பி.எச்.பி ===
=== பி.எச்.பி ===
<source lang=php>
<syntaxhighlight lang=php>
<?php
<?php


வரிசை 57: வரிசை 56:


?>
?>
</syntaxhighlight>
</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:
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>
<syntaxhighlight lang=perl>
package Animal;
package Animal;
sub new {
sub new {
my ($class, $name) = @_;
my ($class, $name) = @_;
bless {name => $name}, $class;
bless {name => $name}, $class;
}
}


package Cat;
package Cat;
வரிசை 92: வரிசை 91:
# Mr. Mistoffelees: Meow
# Mr. Mistoffelees: Meow
# Lassie: Woof! Woof!
# Lassie: Woof! Woof!
</syntaxhighlight>
</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):
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>
<syntaxhighlight lang=perl>
{
{
package Animal;
package Animal;
வரிசை 139: வரிசை 138:
# Lassie: talk => Woof! Woof!
# Lassie: talk => Woof! Woof!
# Lassie: likes => Bone
# Lassie: likes => Bone
</syntaxhighlight>
</source>



== வெளி இணைப்புகள் ==
== வெளி இணைப்புகள் ==

10:58, 18 மே 2015 இல் நிலவும் திருத்தம்

பொருள் நோக்கு நிரலாக்கத்தில், பல்லுருவாக்கம் (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 Perl is inherently straightforward to write because of the language's use of sigils and references. This is the Animal example in standard OO 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!

This means that Perl can also apply polymorphism to the method call. The example below is written using the Moose module in order to show modern OO practises in Perl (it is not required for method polymorphism):

{
    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

வெளி இணைப்புகள்