ãã³ã¼ãã£ã³ã°ãæ¯ããæè¡ãp.196ãp.198ã®è£è¶³
æèãã³ã¼ãã£ã³ã°ãæ¯ããæè¡ãã®11ç« ã§ã¯ããªãã¸ã§ã¯ãæåãå¿ é ã§ã¯ãªãè¨èªã使ã£ã¦ãä¸æ©ãã¤ãªãã¸ã§ã¯ãæåã«è¿ã¥ãã¦ãããã¨ã§ãªãã¸ã§ã¯ãæåãç解ãããã¨ãã¦ãã¾ãã
ã¨ãããä¸çªé£ããã¨ãããçµãã£ã¦ããã¨ããã®ããæå¾ã®ä¸æ© p.196ãp.198 ã®èª¬æãéã«ãªã£ã¦ãã¾ã£ãããã§ããp.196ãp.198ã§ã¯é段ã«ä¾ããã¨3ã¤ã®æ®µå·®ãããã¾ãããããæèã§ã¯1段é£ã°ãã¦2ã¤ãã説æãã¦ãã¾ãããããã¯ã¤ã¾ããã®ãã¨ãªã®ã§ãããã§è£è¶³èª¬æããã¾ã
1段ç®
p.196ã§ã¯ä»¥ä¸ã®ã³ã¼ãã«ãã©ãã¤ãã¾ããããããé段ã®1段ç®ã§ãããã®ã³ã¼ãããæ¯ãèããå¤ããã«æ¸ãæãã¦ã¿ã¾ãããã
# Perl { package Counter; sub new{ return {"value" => 0}; } sub push{ my $values = shift; $values->{count}++; print "$values->{count}å¹\n"; } } { # åæåã®å¦çãããã±ã¼ã¸ã«å ¥ãã my $counter = Counter::new; my $c2 = Counter::new; # å¼æ°ã«ããã·ã¥ã渡ã Counter::push($counter); #-> 1å¹ Counter::push($counter); #-> 2å¹ Counter::push($c2); #-> 1å¹ Counter::push($counter); #-> 3å¹ Counter::push($c2); #-> 2å¹ }
2段ç®
p.197ã§ã¯blessã使ããã¨ã§ãpushã®ãã³ã«ãCounter::ããæ¸ãæéãçãããã¨ã解説ãã¾ããã
次ã«ãã®blessã使ãåæåå¦çãnewã®ä¸ã«å
¥ããã³ã¼ããp.198ã§ç´¹ä»ãã¾ããã
ã¨ããããããã§é段ã1段é£ã°ãã¦ãã¾ããããã§ãã¾ãã¯1段ã ãé²ãã§ã¿ã¾ãããã
# Perl # æ¬æ¸ã«ãªããµã³ãã«ã³ã¼ããp.196ã¨p.198ã®éã«ç¸å½ã { package Counter; sub new{ my $values = {count => 0}; bless $values, "Counter"; } sub push{ my $values = shift; $values->{count}++; print "$values->{count}å¹\n"; } } { # åæåã®å¦çãããã±ã¼ã¸ã«å ¥ãã my $counter = Counter::new; my $c2 = Counter::new; # å¼æ°ã«ããã·ã¥ã渡ã $counter->push; #-> 1å¹ $counter->push; #-> 2å¹ $c2->push; #-> 1å¹ $counter->push; #-> 3å¹ $c2->push; #-> 2å¹ }
1段ç®ã®ã³ã¼ãã¨ã®å·®åã¯ãã®2ç®æã§ãã
5c6,7 < return {"value" => 0}; --- > my $values = {count => 0}; > bless $values, "Counter"; 20,24c22,26 < Counter::push($counter); #-> 1å¹ < Counter::push($counter); #-> 2å¹ < Counter::push($c2); #-> 1å¹ < Counter::push($counter); #-> 3å¹ < Counter::push($c2); #-> 2å¹ --- > $counter->push; #-> 1å¹ > $counter->push; #-> 2å¹ > $c2->push; #-> 1å¹ > $counter->push; #-> 3å¹ > $c2->push; #-> 2å¹
åã®ã³ã¼ãã§ã¯Counter::push($counter);ã¨æ¸ãã¦ãããã¨ãã$counter->push;ã¨æ¸ããããã«ãªãã¾ããã
ããããmy $counter = Counter::new;ã§ã¯ã¾ã ã::ãã使ã£ã¦ãã¾ããããã¯å¤§ããåé¡ã§ã¯ç¡ãã®ã§ãããã::ãã¨ã->ãã使ãåããã®ã¯å«ã ãªããã¨æã£ãã¨ãã¾ãããããããã->ãã«å¤ãããã¨ãã§ããã§ããããï¼
ã§ãã¾ãããããé段ã®3段ç®ã«ãªãã¾ãã
3段ç®
p.198ã®ã³ã¼ãã¯ãnewã®ä¸ã ãããæ²è¼ãã¦ãã¾ããã§ãããå ¨ä½ã¯ãããªãã¾ãã
# Perl { package Counter; sub new{ my $class = shift; my $values = {count => 0}; bless $values, $class; } sub push{ my $values = shift; $values->{count}++; print "$values->{count}å¹\n"; } } { # åæåã®å¦çãããã±ã¼ã¸ã«å ¥ãã my $counter = Counter->new; my $c2 = Counter->new; # å¼æ°ã«ããã·ã¥ã渡ã $counter->push; #-> 1å¹ $counter->push; #-> 2å¹ $c2->push; #-> 1å¹ $counter->push; #-> 3å¹ $c2->push; #-> 2å¹ }
2段ç®ã®ã³ã¼ãã¨ã®å·®åã¯3ç®æã§ãã
5a5 > my $class = shift; 7c7 < bless $values, "Counter"; --- > bless $values, $class; 18,19c18,19 < my $counter = Counter::new; < my $c2 = Counter::new; --- > my $counter = Counter->new; > my $c2 = Counter->new;
ã¾ãCounter::newã§ã¯ãªãCounter->newã§å¼ã³åºãããã«ãã¾ãããããããã¨newã®ç¬¬1å¼æ°ã¨ãã¦Counterã渡ãããããã«ãªãã¾ããããã§shiftã§ãããåãåºãã¦ãblessã®éã«ä½¿ã£ã¦ãã¾ãã
3段ç®ã¯å¿ è¦ãªã®ãï¼
ããã®3段ç®ã¯å¿ è¦ãªã®ãï¼newã¨pushã§å¼ã³åºãæ¹ãéã£ã¦ãè¯ãã®ã§ã¯ï¼ãã¨ãã質åãåºãããããã¾ãããããã®éããå¼ã³åºãæ¹ãåãã§ããå¿ è¦ã¯ããã¾ãããããªãã¸ã§ã¯ãã«çµã³ã¤ãã¦ããã¡ã½ããã¨ãããã§ãªãã¡ã½ããã¯å¥ã®ãã®ãã ããå¼ã³æ¹ãéããã¨ããè¨è¨ã«ãã¦ãããã£ãã®ã§ããå®éãC++ã§ã¯ãããªã£ã¦ãã¾ãã以ä¸ã®ãµã³ãã«ã§ããªãã¸ã§ã¯ãã«çµã³ã¤ãã¦ããmy_instance_methodã¯x.my_instance_methodã¨å¼ã³åºããã¦ãã¾ãããçµã³ã¤ãã¦ããªãmy_static_methodã¯MyClass::my_static_methodã¨å¼ã³åºããã¦ãã¾ãã
#include<iostream> class MyClass { public: static void my_static_method(){ std::cout << "I'm static method!" << std::endl; } void my_instance_method(){ std::cout << "I'm instance method!" << std::endl; } }; int main(){ MyClass::my_static_method(); // -> I'm static method! MyClass x; x.my_instance_method(); // -> I'm instance method! }
ä¸æ¹ãJavaãPythonã§ã¯ã©ã¡ããããªãªãã§å¼ã³åºãã¾ãã
// Java class MyClass { static void my_static_method(){ System.out.println("I'm static method!"); } void my_instance_method(){ System.out.println("I'm instance method!"); } public static void main(String[] args){ MyClass.my_static_method(); // -> I'm static method! MyClass x = new MyClass(); x.my_instance_method(); // -> I'm instance method! } };
# Python class MyClass(object): @staticmethod def my_static_method(): print "I'm static method!"; def my_instance_method(self): print "I'm instance method!"; MyClass.my_static_method() # -> I'm static method! x = MyClass() x.my_instance_method() # -> I'm instance method!
ããã°ã©ãã³ã°è¨èªã®è¨è¨è ã¯ãããããèªåãè¯ãã¨æãããã«ã«ã¼ã«ãè¨è¨ãã¾ãããªã®ã§ãã¡ã½ããã®å¼ã³åºãæ¹ãã©ããããã¯ãè¨èªã«ãã£ã¦ç°ãªãããã§ãã
Perlã§ã¯æ
£ç¿çã«ãå¾è
ã®ãåãæ¹æ³ã§å¼ã³åºãããé¸ã°ãã¦ãã¾ããåèæç®: http://perldoc.perl.org/perlobj.html
ãããã«
ãã®ã¨ã³ããªã¼ã§ã¯p.196ãp.198ã®å 容ã解説ãã¾ããã
æèãã³ã¼ãã£ã³ã°ãæ¯ããæè¡ãã®èªè ã®ã¿ãªããããé ããã質åã»ãææ³ã«ã¯ããã®ãããªæãã§è£è¶³è¨äºãæ¸ãã¦è¡ãããã¨æã£ã¦ãã¾ãããããããªãã質åã»ãææ³ããå¯ãä¸ããã
æèã«é¢ããä»ã®ã¨ã³ããªã¼ã¯ããã³ã¼ãã£ã³ã°ãæ¯ããæè¡ãèè
å
¬å¼ãã¼ã¸ããããã©ããããã«ãã¾ãã