Today, the original stable mood was touched by some things, and the feeling of deep-buried things being pulled up is always painful, not to mention too much, or concentrate on WritingArticleRight. Let's talk about the keywords in this article and see if you can use them: class hierarchies, accessors, attributes, class variables. To put it bluntly, today is the deep in Ruby class, but this deep is not deep enough, just on my own superficial deep.
After reading the ruby syntax, I found that Ruby did not provide the interface syntax, but replaced it with a module. This means that Ruby does not implement multi-inheritance, that is, you can only have one parent class and multiple child classes. Only one superclass and each subclass. Let's take a look at the syntax inherited by ruby class:
Class Man Def Initialize (aname, Aage) @ name = aname @ age = Aage End Def Get_name Return @ Name End Def Get_ageReturn @ Age End Def To_s Return 'Good !!! ' End End Class Superman <man Def Initialize (aname, Aage, Apower) Super (Aname, Aage) @ power = Apower End Def Get_power Return @ Power End Def Set_power Return @ Power End End Supermanabc = Superman. New ('jesse ', 21,100) Puts (Supermanabc. get_power) Puts (Supermanabc. Inspect)
In this example, we can see that the syntax inherited from the class is a <symbol, which is very concise. ReadCodeIt is always clear. Just focus on the Super () access to the superclass, that is, subclass calls the superclass constructor.
When writing Ruby class attributes, if we always write functions such as get_power and set_power, class is required for this attribute operation. get_power is very troublesome. Can we convert it into accessing class directly like this. power = X, it's not so refreshing. Let's take a look at the code to understand Ruby's accessor method:
ClassSuperman <manDefInitialize (aname, Aage, Apower)Super(Aname, Aage) @ power = ApowerEndDefPowerReturn@ PowerEndDefPower = (Apower) @ power = ApowerEndEndSupermanabc = Superman. New ('jack', 21,100) supermanabc. Power = 98Puts(Supermanabc. Power)Puts(Supermanabc. Inspect)
After modifying the Superman class above and defining the accessor of power, it is obvious that the syntax is unnecessary. However, it may be difficult to write each attribute in this way. Continue to simplify the process and generate the next leading attribute reader and writer tag. Take a look at the following code:
ClassSuperman <man attr_reader: Power attr_writer: PowerDefInitialize (aname, Aage, Apower)Super(Aname, Aage) @ power = ApowerEndEnd
It is more simplified. In fact, it can be more simplified. Continue to provide the Code:
ClassSuperman <man attr_accessor: PowerDefInitialize (aname, Aage, Apower)Super(Aname, Aage) @ power = ApowerEndEnd
In fact, I personally recommend the above attr_reader and attr_writer, because you can use the def power method to conveniently process attributes and then return them, you can see the following code:
ClassMan attr_writer: NameDefInitialize (aname, Aage) @ name = aname @ age = AageEndDefNameReturn@ Name. capitalizeEndEndSuperman = man. New ("Jesse", 21)Puts(Superman. Name)
Here, @ name. capitalize writes the first letter of the attribute value to the output. I felt that this was more flexible and I found a compromise between flexibility and convenience. Class variables is a variable shared among all class instances. Let's take a look at the Code:
ClassMan attr_writer: Name @ man_name = 0DefInitialize (aname, Aage) @ name = aname @ age = Aage @ man_name + = 1EndDefNameReturn@ Name. capitalizeEndDefGet_man_nameReturn@ Man_nameEndEndSuperman = man. New ("Jesse", 21) superman1 = man. New ("Jesse1", 22) superman2 = man. New ("Jesse2", 22)PutsSuperman. get_man_namePutsSuperman1.get _ man_namePutsSuperman2.get _ man_name
The syntax of class variables is @ varname.ProgramThe output results are all 3, which means they are shared. This is useful in some places, so you may consider it more.
Write it here today. Let's talk about the hash and array contents tomorrow. By the way, our dormitory staff also fell in love and wished them good luck. Haha, I don't know how to do it, and my heart is suddenly lost. Maybe my view of love has changed...
PS: Today I have read a little book of Ruby and Ruby on Rails: up and running, I feel that I can still get used to English reading with a strong understanding of English. Make a decision today. In the future, if there is an English version of technical books, try to see the English version.