Object-oriented Perl 1

xiaoxiao2021-03-06  36

Object-oriented Perl

Opening

3.1 Three rules

First, create a package.

To write a class with Perl, first create a package. The package in Perl has some classes:

* Package can set a set of code

* Pack your namespace

Second, the method in Perl is Subroutine

If there is a variable $ refhero pointing to a Hero class, the Hero class has a method of fition, fly, etc., through-> can access these methods. $ refhero-> fly; $ refhero-> fight;

Use -> Access Method in accordance with Perl's consistent usage:

$ refhash -> {key}; # get the value of Haxi

$ REFARRAY -> [$ index]; # acquired elements in array

$ REFSUB -> (@ args); # Access a subroutine

$ REFOBJ-> Method (@args); # 类

TIPPS: If you read Reference unfamiliar, you must write a small program with XML :: Simple, you must have a deep understanding.

The method can be used as follows, but there is a difference between the two:

$ refhero-> fly ("5km");

Or

Hero :: fly ("5km");

The parameters pass to the fly are actually ($ refhero, "5km"), while the latter is like an ordinary SUB, only ("5km") is only passed.

Understand this code below is well understood

Package Hero;

SUB FLY

{

MY ($ Self, @args) = @_;

}

With $ SELF, you can access the stuff in your namespace. Because in Perl, the passage of the parameters is Call by reference, so this way of writing is that there is no advantage that it will not change the passing parameters in the SUB (this is often we don't want).

Third, create a ref

Bless completed this. Bless has two parameters: a REF, a string.

转载请注明原文地址:https://www.9cbs.com/read-66820.html

New Post(0)