zoukankan      html  css  js  c++  java
  • Perl OOP

    1. 模块/类(包)

    创建一个名为Apple.pm的包文件(扩展名pm是包的缺省扩展名。意为Perl Module)。
    一个模块就是一个类(包)。

    2. new方法

    new()方法是创建对象时必须被调用的,它是对象的构造函数。

    sub new
    {
        my $class = shift;
        my $this = {};
        bless $this, $class;
        return $this;
    }

    this={}/this。
    bless()函数将类名与引用相关联。从new()函数返回后。$this引用被销毁。

    3. bless方法

    构造函数是类的子程序,它返回与类名相关的一个引用。
    bless()函数将类名与引用相关联。


    其语法为:bless reference [,class]
    reference: 对象的引用
    class: 是可选项。指定对象获取方法的包名,缺省值为当前包名。

    4. 类方法

    有两种方法:静态方法(如:构造函数)和虚方法。


    静态方法第一个參数为类名。虚方法第一个參数为对象的引用。


    虚方法通常首先把第一个參数shift到变量self或this中,然后将该值作普通的引用使用。


    静态方法直接使用类名来调用。虚方法通过该对象的引用来调用。

    5. 继承

    @ISA数组含有类(包)名,@INC数组含有文件路径。


    当一个方法在当前包中未找到时。就到@ISA中去寻找。
    @ISA中还含有当前类继承的基类。

    类中调用的全部方法必须属于同一个类或@ISA数组定义的基类。
    @ISA中的每一个元素都是一个其他类(包)的名字。
    当类找不到方法时。它会从 @ISA 数组中依次寻找(深度优先)。
    类通过訪问@ISA来知道哪些类是它的基类。
    e.g.

    use Fruit;
    our @ISA = qw(Fruit);

    6. OOP Sample

    Super Class: Fruit.pm

    #!/usr/bin/perl -w
    
    use strict;
    use English;
    use warnings;
    
    package Fruit;
    
    sub prompt
    {
        print "
    Fruit is good for health
    ";
    }
    
    1;

    Sub Class: Apple.pm
    Note: Sub class Apple.pm extends super class Fruit.pm via putting Fruit into array @ISA in Apple.pm

    #!/usr/bin/perl -w
    
    use strict;
    use English;
    use warnings;
    
    package Apple;
    
    use Fruit;
    our @ISA = qw(Fruit);
    
    sub new
    {
        my $class = shift;
        my %parm = @_;
    
        my $this = {};
    
        $this -> {'name'} = $parm{'name'};
        $this -> {'weight'} = $parm{'weight'};
    
        bless $this, $class;
    
        $this -> init();
    
        return $this;
    }
    
    sub init
    {
        my $reference = shift;
        my $class = ref $reference;
    
        print "class: $class
    ";
    
        while (my ($key, $value) = each (%$reference))
        {
            print "$key = $value
    ";
        }
    }
    
    sub order
    {
        my $class = ref shift;
        my $customer = shift;
        my $address = shift;
        my $count = shift;
    
        print "
    $customer order $count $class to $address
    ";
    }
    
    1;

    Main Class: Purchase.pl

    #!/usr/bin/perl -w
    
    use strict;
    use English;
    use warnings;
    
    use Apple;
    
    my $apple = new Apple('name' => 'Red Fuji', 'weight' => 300);
    
    $apple -> prompt();
    
    $apple -> order('Zhou Shengshuai', 'ChengDu', 99);

    Output:

    class: Apple
    weight = 300
    name = Red Fuji
    
    Fruit is good for health
    
    Zhou Shengshuai order 99 Apple to ChengDu
  • 相关阅读:
    SDN第一次上机作业
    SDN第一次作业
    2017软件工程第二次作业
    Swift基础(一)
    iOS获取当前屏幕显示的viewcontroller
    Swift基础(二)
    属性和点语法
    Delphi 2008 和 C++Builder 将于8月25日发布
    为什么建议使用多重using
    .Net中Timers的学习备忘二
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7007304.html
Copyright © 2011-2022 走看看