当前位置:  开发笔记 > 编程语言 > 正文

Perl中的对象和类之间有什么区别?

如何解决《Perl中的对象和类之间有什么区别?》经验,为你挑选了3个好方法。

我在解决一个对象和一个类之间的概念差异时遇到了一些麻烦.我真的不明白两者在任何编程语言中的区别,但目前我正在使用Perl和Moose,所以我更喜欢使用这些东西进行解释.

干杯



1> pjf..:

有很多"一个类是一个蓝图,一个对象是从该蓝图构建的东西",但是既然你已经要求使用Moose和Perl的一个具体例子,我想我会提供一个.

在下面的示例中,我们将有一个名为"Hacker"的类.该类(如蓝图)描述了黑客是什么(他们的属性)以及他们可以做什么(他们的方法):

package Hacker;       # Perl 5 spells 'class' as 'package'

use Moose;            # Also enables strict and warnings;

# Attributes in Moose are declared with 'has'.  So a hacker
# 'has' a given_name, a surname, a login name (which they can't change)
# and a list of languages they know.

has 'given_name'       => (is => 'rw', isa => 'Str');
has 'surname'          => (is => 'rw', isa => 'Str');
has 'login'            => (is => 'ro', isa => 'Str');
has 'languages'        => (is => 'rw', isa => 'ArrayRef[Str]');

# Methods are what a hacker can *do*, and are declared in basic Moose
# with subroutine declarations.

# As a simple method, hackers can return their full name when asked.

sub full_name {
    my ($self) = @_;   # $self is my specific hacker.

    # Attributes in Moose are automatically given 'accessor' methods, so
    # it's easy to query what they are for a specific ($self) hacker.

    return join(" ", $self->given_name, $self->surname);
}

# Hackers can also say hello.

sub say_hello {
    my ($self) = @_;

    print "Hello, my name is ", $self->full_name, "\n";

    return;
}

# Hackers can say which languages they like best.

sub praise_languages {
    my ($self) = @_;

    my $languages = $self->languages;

    print "I enjoy programming in: @$languages\n";

    return;
}

1;   # Perl likes files to end in a true value for historical reasons.

现在我们已经获得了Hacker ,我们可以开始制作Hacker 对象了:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

use Hacker;    # Assuming the above is in Hacker.pm

# $pjf is a Hacker object

my $pjf = Hacker->new(
    given_name => "Paul",
    surname    => "Fenwick",
    login      => "pjf",
    languages  => [ qw( Perl C JavaScript) ],
);

# So is $jarich

my $jarich = Hacker->new(
    given_name => "Jacinta",
    surname    => "Richardson",
    login      => "jarich",
    languages  => [ qw( Perl C Haskell ) ],
);

# $pjf can introduce themselves.

$pjf->say_hello;
$pjf->praise_languages;

print "\n----\n\n";

# So can $jarich

$jarich->say_hello;
$jarich->praise_languages;

这导致以下输出:

Hello, my name is Paul Fenwick
I enjoy programming in: Perl C JavaScript

----

Hello, my name is Jacinta Richardson
I enjoy programming in: Perl C Haskell

如果我想要我可以拥有尽可能多的Hacker对象,但仍然只有一个Hacker类描述了所有这些对象的工作原理.

祝一切顺利,

保罗



2> cletus..:

类是一种类型(如"SUV").对象是类的实例("David的SUV").


我总是喜欢这种笨拙的形式:你不能和女人(上课)一起睡觉,但你可以和黛比(对象)一起睡觉.当然,根据您的个人喜好进行修改....

3> Axeman..:

Perl的智慧:

package--a规范.一组行为和数据主要用于帮助这些行为.

一个对象通常是"hashref",即通过在包(和继承的行为)的行为规范所允许的特定数据的集合.

现在,hashref可能包含代码引用.在大多数情况下,这是行为.但是对象可以使用该特定行为的唯一方法是由一些继承(或混合)的类行为指定,该行为期望可能存在位于该位置并调用它的coderef.

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有