Perl中的面向对象概念很大程度上基于引用以及匿名数组和哈希。让我们开始学习面向对象Perl的基本概念。
在Perl中定义一个类非常简单。类以最简单的形式对应于Perl软件包。要在Perl中创建一个类,我们首先构建一个包。
Perl软件包在Perl程序中提供了一个单独的名称空间,该名称空间使子例程和变量独立于与其他软件包中的子例程和变量的冲突。
package Person;
要创建类的实例,我们需要一个对象构造函数。此构造函数是在包内定义的方法。大多数程序员选择将此对象构造函数方法命名为new,但是在Perl中可以使用任何名称。
package Person; sub new { my $class=shift; my $self={ _firstName => shift, _lastName => shift, _ssn => shift, }; # Print all the values just for clarification. print "First Name is $self->{_firstName}\n"; print "Last Name is $self->{_lastName}\n"; print "SSN is $self->{_ssn}\n"; bless $self, $class; return $self; }
现在,让我们看看如何创建一个对象。
$object=new Person( "Mohammad", "Saleem", 23234345);
如果不想将任何值分配给任何类变量,则可以在构造函数中使用简单哈希。例如-
package Person; sub new { my $class=shift; my $self={}; bless $self, $class; return $self; }
其他面向对象的语言具有数据安全性的概念,以防止程序员直接更改对象数据,并且它们提供访问器方法来修改对象数据。 Perl没有私有变量,但是我们仍然可以使用辅助方法的概念来操作对象数据。
让我们定义一个辅助方法来获取人的名字-
sub getFirstName { return $self->{_firstName}; }
另一个用于设置人的名字-
sub setFirstName { my ( $self, $firstName )=@_; $self->{_firstName}=$firstName if defined($firstName); return $self->{_firstName}; }
现在让我们看一个完整的示例:将Person包和辅助函数保存在Person.pm文件中。
#!/usr/bin/perl package Person; sub new { my $class=shift; my $self={ _firstName => shift, _lastName => shift, _ssn => shift, }; # Print all the values just for clarification. print "First Name is $self->{_firstName}\n"; print "Last Name is $self->{_lastName}\n"; print "SSN is $self->{_ssn}\n"; bless $self, $class; return $self; } sub setFirstName { my ( $self, $firstName )=@_; $self->{_firstName}=$firstName if defined($firstName); return $self->{_firstName}; } sub getFirstName { my( $self )=@_; return $self->{_firstName}; } 1;
现在让我们如下使用employee.pl文件中的Person对象-
#!/usr/bin/perl use Person; $object=new Person( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName=$object->getFirstName(); print "Before Setting First Name is : $firstName\n"; # Now Set first name using helper function. $object->setFirstName( "Mohd." ); # Now get first name set by helper function. $firstName=$object->getFirstName(); print "Before Setting First Name is : $firstName\n";
当我们执行以上程序时,它产生以下输出-
First Name is Mohammad Last Name is Saleem SSN is 23234345 Before Setting First Name is : Mohammad Before Setting First Name is : Mohd.
例如,我们可以有一个Employee类,该类继承自Person。这被称为" isa"关系,因为员工是一个人。 Perl有一个特殊的变量@ISA,以帮助解决此问题。 @ISA控制(方法)继承。
因此,要创建一个新的Employee类以继承Person类的方法和属性,我们只需编写以下代码:将这些代码保存在Employee.pm中。
#!/usr/bin/perl package Employee; use Person; use strict; our @ISA=qw(Person); # inherits from Person
现在,Employee Class具有从Person类继承的所有方法和属性,您可以按如下方式使用它们:使用main.pl文件对其进行测试-
#!/usr/bin/perl use Employee; $object=new Employee( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName=$object->getFirstName(); print "Before Setting First Name is : $firstName\n"; # Now Set first name using helper function. $object->setFirstName( "Mohd." ); # Now get first name set by helper function. $firstName=$object->getFirstName(); print "After Setting First Name is : $firstName\n";
当我们执行以上程序时,它产生以下输出-
First Name is Mohammad Last Name is Saleem SSN is 23234345 Before Setting First Name is : Mohammad Before Setting First Name is : Mohd.
子类Employee从父类Person继承所有方法。但是,如果您想在子类中覆盖这些方法,则可以通过提供自己的实现来实现。您可以在子类中添加其他函数,也可以在其父类中添加或修改现有方法的函数。可以按照以下步骤完成:修改Employee.pm文件。
#!/usr/bin/perl package Employee; use Person; use strict; our @ISA=qw(Person); # inherits from Person # Override constructor sub new { my ($class)=@_; # Call the constructor of the parent class, Person. my $self=$class->SUPER::new( $_[1], $_[2], $_[3] ); # Add few more attributes $self->{_id} =undef; $self->{_title}=undef; bless $self, $class; return $self; } # Override helper function sub getFirstName { my( $self )=@_; # This is child class function. print "This is child class helper function\n"; return $self->{_firstName}; } # Add more methods sub setLastName{ my ( $self, $lastName )=@_; $self->{_lastName}=$lastName if defined($lastName); return $self->{_lastName}; } sub getLastName { my( $self )=@_; return $self->{_lastName}; } 1;
现在,让我们再次尝试在main.pl文件中使用Employee对象并执行它。
#!/usr/bin/perl use Employee; $object=new Employee( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName=$object->getFirstName(); print "Before Setting First Name is : $firstName\n"; # Now Set first name using helper function. $object->setFirstName( "Mohd." ); # Now get first name set by helper function. $firstName=$object->getFirstName(); print "After Setting First Name is : $firstName\n";
当我们执行以上程序时,它产生以下输出-
First Name is Mohammad Last Name is Saleem SSN is 23234345 This is child class helper function Before Setting First Name is : Mohammad This is child class helper function After Setting First Name is : Mohd.
Perl提供了其他任何编程语言都找不到的函数:默认子程序。这意味着,如果定义一个名为 AUTOLOAD的函数,那么对未定义子例程的任何调用都会自动调用AUTOLOAD函数。缺少的子例程的名称可以在该子例程中以$AUTOLOAD的形式访问。
默认的自动加载函数对于错误处理非常有用。这是实现AUTOLOAD的示例,您可以以自己的方式实现此函数。
sub AUTOLOAD { my $self=shift; my $type=ref ($self) || croak "$self is not an object"; my $field=$AUTOLOAD; $field =~ s/.*://; unless (exists $self->{$field}) { croak "$field does not exist in object/class $type"; } if (@_) { return $self->($name)=shift; } else { return $self->($name); } }
如果您以前使用面向对象编程进行编程,那么您将意识到有必要创建析构函数,以在使用完对象后释放分配给该对象的内存。一旦对象超出范围,Perl就会自动为您执行此操作。
析构函数方法只是名为DESTROY的成员函数在以下情况下会自动调用-
例如,您可以将以下方法DESTROY放在您的类中-
package MyClass; ... sub DESTROY { print "MyClass::DESTROY called\n"; }
这是另一个不错的示例,它将帮助您理解Perl的面向对象概念。将此源代码放入任何perl文件中并执行它。
#!/usr/bin/perl # Following is the implementation of simple Class. package MyClass; sub new { print "MyClass::new called\n"; my $type=shift; # The package/type name my $self={}; # Reference to empty hash return bless $self, $type; } sub DESTROY { print "MyClass::DESTROY called\n"; } sub MyMethod { print "MyClass::MyMethod called!\n"; } # Following is the implemnetation of 遗产. package MySubClass; @ISA=qw( MyClass ); sub new { print "MySubClass::new called\n"; my $type=shift; # The package/type name my $self=MyClass->new; # Reference to empty hash return bless $self, $type; } sub DESTROY { print "MySubClass::DESTROY called\n"; } sub MyMethod { my $self=shift; $self->SUPER::MyMethod(); print " MySubClass::MyMethod called!\n"; } # Here is the main program using above classes. package main; print "Invoke MyClass method\n"; $myObject=MyClass->new(); $myObject->MyMethod(); print "Invoke MySubClass method\n"; $myObject2=MySubClass->new(); $myObject2->MyMethod(); print "Create a scoped object\n"; { my $myObject2=MyClass->new(); } # Destructor is called automatically here print "Create and undef an object\n"; $myObject3=MyClass->new(); undef $myObject3; print "Fall off the end of the script...\n"; # Remaining destructors are called automatically here
当我们执行以上程序时,它产生以下输出-
Invoke MyClass method MyClass::new called MyClass::MyMethod called! Invoke MySubClass method MySubClass::new called MyClass::new called MyClass::MyMethod called! MySubClass::MyMethod called! Create a scoped object MyClass::new called MyClass::DESTROY called Create and undef an object MyClass::new called MyClass::DESTROY called Fall off the end of the script... MyClass::DESTROY called MySubClass::DESTROY called
Perl - 面向对象 - 无涯教程网无涯教程网提供 Perl中的面向对象概念很大程度上基于引用以及匿名数组和哈希。让我们开始学习面向对...https://www.learnfk.com/perl/perl-object-oriented.html