Perl object-oriented

Source: Internet
Author: User
Perl object-oriented first let's take a look at the three basic definitions of Perl Object-Oriented Programming: 1. an "object" refers to a simple reference that "there is a way to know which class it belongs. (An object is a reference variable) 2. A "Class" refers to a simple package that "can provide some methods to its objects. (Class is the package) 3. A "method" refers to a simple sub-statement that "accepts an object or class name as the first parameter" Program . (The class method is the method of the first parameter for the class name or object) a class is just a simple package and C ++ is different. Perl does not provide any special syntax for class definition. In fact, the class is just a package. You can use a package as a class and use the function in the package as a class method. However, there is a special array called @ Isa, which indicates "Where should Perl continue to find the desired method when perl cannot find it in the current package ". This is the key to implementing "inheritance" in Perl. @ Every element in ISA is the name of another package. When the class cannot find the method, it will search for it in sequence from the @ ISA array (depth first ). Class to know which classes are its base classes by accessing @ Isa. All classes have an implicit base class (ancestor class): "universal ". The "universal" class provides several common class methods for its subclass. It provides the following methods: ISA, can. ISA is used to determine whether a variable is inherited from a class. The parameter after can is a method to determine whether the method is defined in this class or base class. In addition, you can add a new method to universal. Once a new method is added to it, all classes can be called. Example of adding a new method to unibersal: Sub universal: log () {My ($ self, $ MSG) =@_; print "$ self: $ MSG \ n ";} in this way, you can call this function in each class and print the class name before log. The object only references the constructor in Perl and is just a sub-program. This sub-program returns a reference processed by bless. The reference processed by bless is what people call "object ", the role of bless is to indicate which class the object belongs ". The simplest constructor is package critter; sub new {bless {}}. If you want users to call your constructor in the form of "Class-> New, if you can call it in the form of "$ obj-> New ()", do this: Sub new {my $ this = shift; my $ class = REF ($ this) | $ this; my $ self ={}; bless $ self, $ class; $ self-> initialize (); return $ self;} A method is a simple subroutine method that treats the object or class name when it is called as its first parameter. There are two different ways to call Methods: "Call class method" and "Call instance method ". The class method regards the class name as the first parameter. It provides functions for classes, rather than for a specific object. The constructor is usually a class method. Most class methods simply ignore the first parameter, because the method knows what class it is in and does not care what class it calls. Perl provides two different ways to call a method. The simplest form is the arrow Symbol: My $ Fred = critter-> Find ("Fred"); $ Fred-> display ("height", "weight "); you can be familiar with the referenced "->" operator for a long time. In fact, because the above $ Fred is a reference pointing to an object, you can also consider the arrow operator as another form of unreference. The reference or class name that appears on the left of the arrow is passed as the first parameter to the method on the right of the arrow. So the above Code It is equivalent to: My $ Fred = critter: Find ("critter", "Fred"); critter: Display ($ Fred, "height", "weight "); simple Example
1 Description
This program demonstrates how to perform Object-Oriented Programming in Perl: the demo contains two files: person. PL and person. PM, put person. PL and person. put PM in the current directory and run " Perl person. pl " You can see the result. Person. PM defines a class named person. Person. pl creates an instance of the person class and tests the member method of the person class.

2, Person. PM content
#! /Usr/bin/perl-W
PackagePerson;
UseStrict;

Sub New {
My $ Class = Shift ();
Print ( " Class = $ class \ n " );
My $ Self = {};
$ Self -> { " Name " } = Shift ();
$ Self -> { " Sex " } = Shift ();
Bless $ Self , $ Class ;
Return $ Self ;
}

SubGetname {
My($ Self) =@_;
Return $ Self-> {"Name"};
}

sub setname {
my ( $ self , $ name ) = @_;
$ self -> {" name " }= $ name ;< BR >}

sub getsex {
my ( $ self ) = @_;
return $ self -> {" sex " };
}

SubSetsex {
My($ Self,$ Sex) =@_;
$ Self-> {"Sex"} =$ Sex;
}

3, Person. pl content
#! /Usr/bin/perl-W
UseStrict;
UsePerson;

Sub Main ()
{
My $ Tom = Person-> New ( " Tom " , " Male " );
My $ Kiss = Person-> New ( " Kiss " , " Female " );
My @ Persons = ( $ Tom , $ Kiss );
For My $ P ( @ Persons ){
Printf ( " Name: % s \ tsex: % s \ n " , $ P -> Getname (), $ P -> Getsex ());
}
}

& Main ();

4Program running result
Class = person
Class = person
Name: Tom Sex: Male
Name: Kiss sex: female from:

Http://bbs.chinaunix.net/thread-980909-1-1.html

Http://bbs.chinaunix.net/forum.php? MoD = viewthread & tid = 770196.

 

Complete!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.