Ruby's class variables and class methods

Source: Internet
Author: User

Like most object-oriented languages, ruby classes also allow you to define class variables and methods. A class variable allows a single variable to be shared among all instances of a class. In Ruby, two @@ numbers are used to indicate class variables. For example, if you want to make all instances of a BankAccount class share the same interest rate, the class might be defined as follows:

Class BankAccount
@ @interestRate = 6.5
def bankaccount.getinterestrate ()
@ @interestRate
End Attr_accessor:balance
def initialize (bal)
@balance = Bal
End

As you can see, class variables must be initialized before use, and like instance variables, you need to write accessor methods if you want to access class variables. Here, I define a class method to return the interest rate. Note that the class name and the period preceding the getinterestrate represent a class method. A class method, regardless of any instance, works the same way-this is to return the same interest rate to all bankaccount instances. To invoke a class method, you need to use the class name as it would in the class method definition:

IRB (main):045:0> bankaccount.getinterestrate
=> 6.5

In fact, the "new" method for creating instances of a class is a class method. So, when you type "Rectangle.new" into your program, you actually call the new class method-This is what Ruby provides by default.

Inherited

One of the principles of object-oriented programming is to support class hierarchies. Like the sort of things in nature, classes allow inheritance from more general classes. The features of object-oriented programming are mainly embodied in the use of methods and variables. For example, a square class inherits some of the characteristics of the rectangle class, such as methods and variables. A square is a more specific type of rectangle (an rectangle instance of equal height and width), but it still has a height and width and an area (and is the same as the calculation method of the rectangle). In Ruby, the square class can be created using the following definitions:

Class Square < Rectangle end

"<rectangle" means that square is a subclass of Rectangle, or, conversely, that Rectangle is a superclass of square. By default, a square instance automatically owns the same properties and methods that all of the rectangle have, including the Height,width and area methods. To ensure that the side length of the square instance is equal, you can overload the existing square Initialize method:

Class Square < Rectangle
def initialize (size)
@height = size
@width = size
End

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.