×Ô¶¯»¯²âÊÔ֮·£¨Èý£© rubyÀïµÄgetÓëset·½·¨
ÕÕÀý¿ÉÒÔÏÈ¿´¶Ë³ÌÐò
class Person
def initialize( name,age=18 )
@name = name
@age = age
@motherland = "China"
end
def talk
puts "my name is "+@name+", age is "+@age.to_s
if @motherland == "China"
puts "I\'m Chinese."
else
puts "I\'m foreigner."
end
end
attr_writer :motherland
end
p1=Person.new("kaichuan",20)
p1.talk
p2=Person.new("Ben")
p2.motherland="ABC"
p2.talk
³ÌÐòÔËÐнá¹û
my name is kaichuan, age is 20
I'm Chinese.
my name is Ben, age is 18
I'm foreigner.
>Exit code: 0
ÓйýÃæÏò¶ÔÏó±à³ÌÓïÑÔ¾ÀúµÄͯЬºÜÈÝÒ׿ÉÒÔ¿´Ã÷°×£¬µ«ÊÇÀïÃæÖµµÄÒ»ÌáµÄÊÇÕâ¾ä£º“attr_writer :motherland”
Õâ¾ä»°Ï൱ÓÚÎÒÃÇÃæÏò¶ÔÏó±à³ÌÓïÑÔµÄset·½·¨£¬Ò²¿ÉÒÔÕâÑùд
def motherland=(value)
return @motherland =value
end
ÄÇôÏàÓ¦µÄget·½·¨Îª attr_ reader :motherland
Ï൱ÓÚ
def motherland
return @motherland
end
ÏàÓ¦µÄ attr_accessor :motherland Ï൱ÓÚattr_reader:motherland£» attr_writer :motherland
Ïà¹ØÎĵµ£º
¹«Ë¾ÀïÔ½À´Ô½¶àµÄ½Ó¿ÚÒÔICE·½Ê½ÌṩÁË£¬ÒÔǰû½Ó´¥¹ý£¬ËùÒÔÎÒÕâÁ½ÌìÒ²³éÁ˵ãʱ¼äÑо¿ÁËÏÂICE£¬²Î¿¼ÁËÍøÉϺܶàÌû×Ó£¬²ÅÖÕÓÚÅÜͨµÚÒ»¸ö¼òµ¥µÄICE
½ÓÏÂÀ´ÌùÉÏÎÒËùÓл·¾³µÄ´î½¨¹ý³ÌÒÔ¼°´úÂ룬ºÍ´ó¼Ò·ÖÏíÏÂICEËù´øÀ´µÄ¿ìÀÖ
ICEÊÇʲôÎҾͲ»¶à˵ÁË
1¡¢ Ò»¿î¸ßÐÔÄܵÄÖмä¼þ£¬»ùÓÚICE¿ÉÒÔʵÏÖµçÐż¶µÄ½ ......
ÐÂÄê¸Õ¿ªÊ¼,ÒѾÓÐһЩÉúÃ͵ÄÅóÓÑÒª¿ªÊ¼ÐµÄÖ°³¡ÉúÑÄÁË,
²»ÖªµÀ»á²»»áÓöµ½ÕâÑùµÄÎÊÌâ: ÏëÕÒÒ»·ÝRuby on railsµÄ¹¤×÷,ÎÒÓ¦¸ÃÖªµÀÄÄЩ֪ʶ
?
»¹ºÃ,ÎÒ²¢²»ÊÇÏëÖ¸µãÄãʲô,Ö»ÊÇÏë°´ÎÒµÄ˼ά·½Ê½Õ¹¿ª,ÈÃÄãÄÜ"ÎÅ"µ½µã¶«Î÷¼´³É.
¹ÅÔÆ:Öª¼ºÖª±Ë£¬°ÙÕ½²»´ù£»²»Öª±Ë¶øÖª¼º£¬Ò»Ê¤Ò»¸º£» ......
Ëɱ¾ÐкêÈçÔ¼ÓÚÊ¥µ®½Ú·¢²¼ÁËRuby 1.9¡£¸ù¾ÝRubyµÄ¹ßÀý£¬Ð¡ÊýµãºóÃæµÚһλÈç¹ûÊǵ¥Êý£¬ÄÇô¾Í±íÃ÷ÕâÊÇÒ»¸öʵÑé°æ±¾£¬²»ÍƼöÓÃÓÚ²úÆ·»·¾³¡£Ëùν“²úÆ·»·¾³”£¬¶ÔÓÚÄ¿Ç°µÄRubyÀ´Ëµ£¬»ù±¾ÉϾÍÊÇRuby on Rails¡£´ÓÄ¿Ç°RoRÉçȺµÄ·´Ó³À´¿´£¬È·ÊµÓÐÈËÕýÔÚ³¢ÊÔÓÃRuby 1.9ÅäºÏRoR£¬µ«ÊÇÉÐÊô ......
½ñÌì°ÑRuby Shoes´ò°üµÄ³ÌÐò¸´ÖƵ½Ã»Óа²×°Ruby ShoesµÄ»úÆ÷ÉÏ£¬·¢ÏÖËü»¹Ï²»ÁËRuby ShoesµÄ»·¾³°ü¡£
ÕâÈÃÎҸоõºÜ¾ÚÉ¥£¬ºÃ²»ÈÝÒ×ÈëÃÅÁËÒ»ÃÅÓïÑÔ£¬È´ÓöÉÏÁËÎÞ·¨²¿ÊðµÄÎÊÌâ¡£
ÉõÖÁ£¬ÎÒºóÀ´ÏëÒª°ÑÔËÐл·¾³Ç¶Èëµ½Ruby ShoesÖУ¬¶¼Ã»Äܹ»×öµ½¡£
ÕâÑùµÄÎÊÌâÕæÊÇÈÃÎҸоõµ½¶ÔShoesµÄʧÍû¡£
²»¹ý»¹ºÃ£¬³öÁËRuby Shoes£¬»¹Óкܶ ......
class Tree
def initialize
puts "Make a normal tree"
end
def decorate
puts "Make sure the tree won\'t fall"
end
end
class RedBalls < Tree
def initialize(tree)
@parent = tree;
end
def decorate
@parent.decorate
puts "Put on some red balls"
end
end
......