我讀Programming Perl
,我發現這個代碼片段:在對象實例上調用new有什麼好處?
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "bay",
legs => 4,
owner => undef,
@_, # Override previous attributes
};
return bless $self, $class;
}
有了這樣一個構造函數,你對一個對象實例調用new
的好處?我認爲這是它的目的,對吧?我的猜測是,如果任何人想寫這樣的構造函數,他將不得不添加一些代碼,將第一個對象的屬性複製到即將創建的對象。
注意:一些非常好的信息在相關問:http://stackoverflow.com/questions/1621373/whats-the-benefit-of-calling-new-on-an-object-instance?rq= 1 – DVK 2013-06-18 14:09:10
大聲笑,我現在正在閱讀同一本書 – 2013-06-19 01:37:28