Ruby Concise Learning Notes (ii): Class inheritance, attributes, class variables _ruby topics

Source: Internet
Author: User
Tags inheritance uppercase letter

1. How to declare a subclass

Copy Code code as follows:

Class Treasure < Thing

So the property name,description in the thing class are treasure inherited

2. What are the parameters of the Initialize method passed into the parent class in the following three ways?

Copy Code code as follows:

# This is passes a, B, C to the superclass
Def initialize (A, B, C, D, E, F)
Super (A, B, c)
End
# This is 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 one a,b,c the parameters into the parent class initialize method; the second passes all parameters without parentheses; the third does not pass in the parameter

3. Setter/getter of attributes

Someone wrote Setter/getter:

Copy Code code as follows:

Puts (t1.get_description)
T1.set_description ("Some description")

This seems more convenient:
Copy Code code as follows:

Puts (t1.description)
T1.description = "Some description"

If you want to use the second way, you have to write this:

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

But this is wrong: def name = (aname)

Do you see anything else on business?

As you can see from the previous chapter, there are two methods defined: Description method and Description= method. The original was implemented by adding "=" to the method name, and Ruby was so magical that Java couldn't write it that way.

However, there are actually simpler ways to achieve setter/getter

Copy Code code as follows:

Attr_reader:d escription
Attr_writer:d escription

Consisting of a attr_reader/attr_writer plus symbol (:d escription), in fact, you can set setter/getter for more than one element at a time
Copy Code code as follows:

Attr_writer (: Name,:d escription)
Attr_accessor (: value,: ID,: Owner)
Attr_accessor

Equivalent to:
Copy Code code 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 (construction method).

In the 2nd, the use of the Super method is introduced, and a single super passes all parameters to the parent class initialize, while super with the parameters passes the specified parameters to the parent class initialize.

Copy Code code as follows:

# This passes Aname and adescription to the superclass
def initialize (aname,adescription)
Super (Aname, adescription)
End

# This is 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 Code code as follows:

Class X
A = 10

Class Y
def XYZ
Puts ("Goodbye")
End
End

def SELF.ABC
Puts ("Hello")
End
End

Constants: Variables that begin with an uppercase letter.

If you want to access constants or inner classes, you need to::

Copy Code code as follows:

Puts (X::A)
X::ABC # You can also use:: To invoke the method
X.ABC # Of course that's okay

OB = X::y.new
Ob.xyz

6. Partial class (Partial Class)

You can modify existing classes in Ruby and affect the objects that have been generated

Copy Code code as follows:

Class A
def a
Puts ' a '
End
End

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

Class A
def b
Puts ' B '
End
End

A.public_methods (False)
# => [: A,: b]//have A and b

What you can't modify, however, is which class the class inherits. Like what

Copy Code code as follows:

Class A
def a
Puts ' a '
End
End

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

# Typeerror:superclass mismatch for class A
# All classes inherit the object class by default, and a also inherits object, so the error occurs when you let a inherit a string

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.