Perl-如何在一个类里面访问一个属性才是正确的?

tommycpma 2006-10-16 03:12:36
Hi all,
代码如下:
#------------------------------------
#myPM.pm
package myPM;

my $aProperty;
sub new
{
my $this = {};
bless $this;
return $this;



}


sub OneSub
{
my $this = shift;
$this->{'aProperty'} = "something";


}


sub TwoSub
{
$aProperty = "something";

}


#------------------------------------------------------------------
OneSub 和 TwoSub 都可以正确设置类里的属性,但哪一个才是正确的呢?

...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2006-10-17
  • 打赏
  • 举报
回复
在文件范围内的变量有点类似于C++中的static变量,而在{}哈希中定义的变量才是一个类实例的真正变量。你所定义的变量(属性)是perl模拟或自身概念上的面向对象设计的成员变量。
tommycpma 2006-10-17
  • 打赏
  • 举报
回复
I got a anwser:
_______________________________________
You have an object property and a class property here. OneSub accesses
(sets) the object property and TwoSub sets the class property. Both
could make sense. Whether you want one or the other (or both) can't
be decided without knowing what you want to do with your class.
_________________________________________________________________________
tommycpma 2006-10-16
  • 打赏
  • 举报
回复
它里面的指示器好像也只是一个函数而已,指示器里面具体怎么写应该还类似我上面的疑问阿
southsky8019 2006-10-16
  • 打赏
  • 举报
回复
据说可以通过指示器访问,我也是刚接触

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧