gpt4 book ai didi

perl - 如何在 Perl 的 getter 方法中返回两个值?

转载 作者:行者123 更新时间:2023-12-05 01:03:20 25 4
gpt4 key购买 nike

use strict;
use warnings;

package LineSegment;

sub new
{
my $class = shift;
my ($ax, $ay, $bx, $by) = @_;
my $self = {"ax"=>$ax,
"ay"=>$ay,
"bx"=>$bx,
"by"=>$by,
};
bless ($self, $class);
return $self;
}
sub getA{
#Issue on get A
my $self = shift;
return ($self->{ax}, $self->{ay});
}
sub getB{
#Issue on get B
my $self = @_;
return ($self->{bx}, $self->{by});
}
sub setA{
#Can print correct value. Is the return statement where it goes wrong?
my($self, $ax, $ay) = @_;
$self->{ax} = $ax if defined($ax);
$self->{ay} = $ay if defined($ay);
print "Setting ax: $self->{ax}\n";
print "Setting ay: $self->{ay}\n";

return ($self->{ax}, $self->{ay});
}
sub setB{
#Can print correct value. Is the return statement where it goes wrong?
my($self, $bx, $by) = @_;
$self->{bx} = $bx if defined($bx);
$self->{by} = $by if defined($by);
return $self->{bx}, $self->{by};
}
1;

我正在尝试创建一个名为 LineSegment 的类。 ax 和 ay 是一个point 和 bx 和 by 也是。我无法让 getA 或 getB 返回我的内容想。他们只返回第二个值,这对 getA 来说是好的并通过 getB。我希望它返回两个值 (ax, ay) 或 (bx,by)。我怎样才能做到这一点?在我的 setA 和 setB 方法中,值将打印。但是,我可以在 setA 和 setB 中将它们返回错误吗?还是我的问题出在我的 getter 方法上?

这是我的主要内容:

print "Starting Line Segment\n";
use LineSegment;
$line = new LineSegment(10,20,30,40);
$line->setA(15,10);
$a = $line->getA();
print "Point A is: $a\n";

Here is my Point class:

use strict;
use warnings;

#class name
package Point;

#constructor
sub new
{
my $class = shift;
my($x, $y) = @_;
my $self = {"x"=>$x,
"y"=>$y,
};
bless ($self, $class);
return $self;
}

sub getX{
my($self) = @_;
return $self->{x};
}
sub setX{
my ($self, $x) = @_;
$self->{x} = $x if defined($x);
return $self->{x};
}
sub setY{
my ($self, $y) = @_;
$self->{y} = $y if defined($y);
return $self->{y};
}
sub random{
my $self = shift;
my $range = 50;
$self->{x} = int(rand($range));
$self->{y} = int(rand($range));

return ($self->{x}, $self->{y});
}
1;

Updated main:

use strict;
use warnings;

use Point;
use LineSegment;

my $line = LineSegment->new(Point->new()->random, Point->new()->random);
my $pointA = $line->getA;
my $pointB = $line->getB;
printf "Point = (%d,%d)\n", $pointA->getX, $pointA->getY;

最佳答案

正如 Tanktalus 所指出的,您正在返回一个包含两个值的列表,并期望能够将其视为单个目的。标量上下文中的列表计算为列表的最后 元素,因此您只获得 Y 坐标

我在下面写了一些功能代码。可能会让您感到困惑的一件事是 hash slice 语法 @{$self}{qw/_x _y/} = @_

$self->{_x} = $_[0];
$self->{_y} = $_[1];

您应该记住在每个 Perl 源文件的顶部use strictuse warnings。您还应该避免使用 $a$b,因为它们在 Perl 内部使用。无论如何,更长、更具描述性的标识符更好

如果我像这样更改您的 Point.pm 以便其构造函数采用参数(我还修复了您的 random 方法)

点.pm

use strict;
use warnings 'all';

package Point;

sub new {
my $class = shift;

my $self = { };
@{$self}{qw/ _x _y /} = @_ if @_;

bless $self, $class;
}

sub getX{
my $self = shift;

return $self->{_x};
}

sub getY{
my $self = shift;

return $self->{_y};
}

sub setX {
my $self = shift;

$self->{_x} = $_[0] if @_;

return $self->{_x};
}

sub setY {
my $self = shift;

$self->{_y} = $_[0] if @_;

return $self->{_y};
}

use constant RANGE => 50;

sub random {
my $self = shift;

$self->{_x} = int rand RANGE;
$self->{_y} = int rand RANGE;

return $self;
}

1;

然后这样写 LineSegment.pm

线段.pm

use strict;
use warnings 'all';

package LineSegment;

sub new {
my $class = shift;

my $self = { };
@{$self}{qw/ _pA _pB /} = @_ if @_;

bless $self, $class;
}

sub getA {
my $self = shift;

return $self->{_pA};
}

sub getB {
my $self = shift;

return $self->{_pB};
}

sub setA {
my $self = shift;

$self->{_pA} = $_[0] if @_;

return $self->{_pA};
}

sub setB {
my $self = shift;

$self->{_pB} = $_[0] if @_;

return $self->{_pB};
}

1;

然后我可以写一个程序来做我认为你想要的事情

主.pl

use strict;
use warnings 'all';

use Point;
use LineSegment;

my $line = new LineSegment(
Point->new(10, 20),
Point->new(30, 40),
);

$line->setA( Point->new(15, 10) );

my $point = $line->getA;

printf "Point = (%d,%d)\n",
$point->getX,
$point->getY;

输出

Point = (15,10)

关于perl - 如何在 Perl 的 getter 方法中返回两个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42283217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com