Ruby Learning-Chapter II

Source: Internet
Author: User
Tags uppercase letter

Chapter II

Class inheritance, properties, class variables

1. How to declare a subclass

class Treasure < Thing

This way the attribute name,description in the thing class is inherited by Treasure

2. What are the parameters of the following three ways to pass in the parent class initialize method?

# This passes a, B and C to the superclass def Initialize (A, B, C, D, E, f) Super (A, B, c) End # This passes a, B and C to the superclass def Initialize (A, B, c) superend # This passes no arguments to the superclass def Initialize (A, B, c) super () End

The first method is to pass the parameter a,b,c into the parent class initialize, the second pass all the parameters, but not the parentheses; the third one does not pass in the parameter

3. Setter/getter of attributes

Someone wrote this setter/getter:


T1.set_description ("Some description")

This seems more convenient:

= "Some description"

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

def Description      return  def description=(adescription)     = adescription End

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

But it's wrong: def name = (aName)

Do you see anything else in the business?

As you can see from the previous chapter, there are two methods defined here: The description method and the Description= method. It turns out that by adding "=" to the method name, Ruby is amazing, and Java cannot write like this.

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

Attr_reader:d escription attr_writer:d escription

Consists of a attr_reader/attr_writer plus symbol (:D escription), which in fact can be set for multiple elements at once Setter/getter


Attr_accessor (: value,: ID,: Owner)

Attr_accessor

Equivalent to:

Attr_reader:value

Attr_writer:value

4.super

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

In the 2nd, the use of the Super method is described, and the individual super passes all parameters to the parent class initialize, while the super with the parameter passes the specified argument to the parent class initialize.

# This passes AName, adescription to the superclass def Initialize (aname,adescription) Super (AName, adescription) End # This passes a, B and C to the superclass ' s Amethod def Amethod (A, B, c) superend

5. Constants and Nested classes (constants & nested Class)

Class= ten  class  Y  def  xyz   "Goodbye  "  )  end end  def  self.abc  puts ("  Hello") endend

Constant: A variable that begins with an uppercase letter.

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

puts (x::a) x::abc         # You can also use:: To invoke Methods X.ABC        #  Of course this can also be  = x::y.newob.xyz

6. Partial classes (partial class)

In Ruby, you can modify existing classes and affect the objects that have already been generated.

 class   A  def   a puts    a     Endenda  = A.newa.public_methods (false)//print A Class All public methods  #   = [: a]//only A  class   A  def   b puts    b     Endenda.public_methods (false)  #   = [: A,: b]//has a and b  

What cannot be modified is which class the class inherits from. Like what

class A   def a     ' a '   endendclass A < String  def  c    ' C '   EndEnd
# Typeerror:superclass mismatch for class A
# All classes inherit the object class by default, and a also inherits object, so you get an error when you let a inherit a string

Ruby Learning-Chapter II

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.