Ruby class inheritance

Source: Internet
Author: User

Ruby-inherited syntax

class DerivedClass < BaseClass    #some stuffend

<As an inherited symbol

Override Concept

Sometimes, we want child classes to inherit from the parent class to do different things from the parent class, so we need to rewrite the method in the Child class. For example, you have a class named "email" that inherits from the class message. Both classes have the send method, but the send method of the e-mail class requires the mail address and a series of mail protocols, however, the send method in the message does not know this. Instead of adding the send_mail Method to the email class, it is better to explicitly modify the send method to meet the needs of the email.

For example:

Class creature def initialize (name) @ name = Name end def fight return "punch to the chops! "Endend # add your code below! Class dragon <creature def fight return "breathes fire! "Endend dragon = dragon. New (" dragon ") dragon. Fight -------------------------------------------------- output:" breathes fire! "

 

On the other hand, sometimes the subclass finds that the method inherited from the parent class has been rewritten. Don't be alarmed. We can directly obtain the response method of the parent class, which requires the super keyword.

Syntax:

class DerivedClass < Base  def some_method    super(optional args)      # Some stuff    end  endend

When you call Super in a method, this tells Ruby to find a function with the same name as the method used to call Super in the parent class. If so, ruby will use this method of its parent class version.

For example:

Class creature def initialize (name) @ name = Name end def fight return "punch to the chops! "Endend # add your code below! Class dragon <creature def fight puts "instead of breathing fire... "Super endend dragon = dragon. new ("W") dragon. fight ------------------------------------------------------------------- output: instead of breathing fire... "Punch to the chops! "

 

Ruby does not support multi-inheritance. However, Ruby allows Mixin. Let's talk about it later.

 

For program security, Ruby allows us to explicitly declare public or private methods. The public method can be called as an interface, while the private method is invisible to the outside world. If public or private is not specified, Ruby is set to public by default.

1 class person 2 def initialize (name, age) 3 @ name = Name 4 @ age = age 5 end 6 7 public # This method can be called from outside the class. 8 9 def about_me10 puts "I'm # {@ name} And I'm # {@ age} years old! "11 end12 13 private # This method can't! 14 15 def bank_account_number16 @ account_number = 1234517 puts "my bank account number is # {@ account_number }. "18 end19 end20 21 Eric = person. new ("Eric", 26) 22 Eric. about_me23 Eric. bank_account_number # error. The private method is called! ------------------------------------------------------------------------- Output: I'm Eric and I'm 26 years old! Private method 'Bank _ account_number 'called for # <context: Person: 0x0000000262d930 @ name = "Eric", @ age = 26>

 

Use attr_reader and attr_writer to read and write attributes)

According to what we learned earlier, if you want to access the attributes defined in the class, for example, if you want to access the @ name instance variable, you must write

def name  @nameend

If we want to modify the @ name instance variable, We need to write it like this:

def name=(value)  @name = valueend

You don't have to worry about it now. We can use attr_reader and attr_writer to read and write variables, as follows:

class Person  attr_reader :name  attr_writer :name  def initialize(name)    @name = name  endend

When you encounter the above Code, Ruby will automatically do something similar to the following:

def name  @nameenddef name=(value)  @name = valueend

Like magic, we can read and write variables at will! We only pass the variable (converted to symbol) to attr_reader and attr_writer.

If you want to read and write a variable, you can also use attr_reader and attr_writer to make it simpler.

 

Ruby class inheritance

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.