Ruby concise learning notes (2): class inheritance, attributes, class variables, and ruby learning notes

Source: Internet
Author: User

Ruby concise learning notes (2): class inheritance, attributes, class variables, and ruby learning notes

1. How to declare a subclass
Copy codeThe Code is as follows:
Class Treasure <Thing

In this way, the property names and descriptions of Thing classes are inherited by Treasure.

2. What are the parameters used to pass in the initialize method of the parent class in the following three methods?
Copy codeThe Code is as follows:
# This passes a, B, c to the superclass
Def initialize (a, B, c, d, e, f)
Super (a, B, c)
End
# This passes a, B, c to the superclass

Def initialize (a, B, c)
Super
End
# This passes no arguments to the superclass
Def initialize (a, B, c)
Super ()
End

The first method is to pass a, B, and c in the parameter into the initialize method of the parent class. The second method is to pass in all parameters without brackets. The third method is not to pass in parameters.

3. setter/getter of the property

Someone writes setter/getter as follows:
Copy codeThe Code is as follows:
Puts (t1.get _ description)
T1.set _ description ("Some description ")

This seems more convenient:
Copy codeThe Code is as follows:
Puts (t1.description)
T1.description = "Some description"

If you want to use the second method, You have:

Note: This is correct: def name = (aName)

But this is wrong: def name = (aName)

Do you see the difference?

As you can see in the previous chapter, two methods are defined: description method and description = method. It was originally implemented by adding "=" to the method name. ruby is amazing and Java cannot write it like this.

However, there are actually simpler methods to implement setter/getter
Copy codeThe Code is as follows:
Attr_reader: description
Attr_writer: description

It consists of an attr_reader/attr_writer with the symbol (: description). In fact, you can set setter/getter for multiple elements at a time.
Copy codeThe Code is as follows:
Attr_writer (: name,: description)
Attr_accessor (: value,: id,: owner)
Attr_accessor

It is equivalent:
Copy codeThe Code is as follows:
Attr_reader: value
Attr_writer: value

4. super

Unlike Java, the super method in Ruby can appear in any method, not just initialize (constructor.

The usage of the super method is introduced in. A separate super will pass all the parameters to the parent class initialize, and the super with parameters will pass the specified parameters to the parent class initialize.

Copy codeThe Code is as follows:
# This passes aName, adetries to the superclass
Def initialize (aName, ADEE)
Super (aName, adegion)
End

# This passes a, B, c to the superclass's aMethod
Def aMethod (a, B, c)
Super
End

5. constants and nested classes (constants & nested class)

Copy codeThe Code is as follows:
Class X
A = 10
 
Class Y
Def xyz
Puts ("goodbye ")
End
End
 
Def self. abc
Puts ("hello ")
End
End

Constant: a variable starting with an uppercase letter.

To access a constant or internal class, you need ::
Copy codeThe Code is as follows:
Puts (X:)
X: abc # You can also use: to call the method.
X. abc # Of course.

Ob = X: Y. new
Ob. xyz

6. Partial Class)

In Ruby, you can modify existing classes and affect generated objects.
Copy codeThe Code is as follows:
Class
Def
Puts 'A'
End
End

A = A. new
A. public_methods (false) // print all public methods of class
# => [: A] // only

Class
Def B
Puts 'B'
End
End

A. public_methods (false)
# => [: A,: B] // There Are a and B

What cannot be modified is the class that the class inherits. For example

Copy codeThe Code is as follows:
Class
Def
Puts 'A'
End
End

Class A <String
Def c
Puts 'C'
End
End

# TypeError: superclass mismatch for class
# By default, all classes inherit the Object class, And A also inherits the Object. Therefore, an error is reported when A inherits the String class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.