Ruby Learning Notes (2) basic use of Classes _ruby topics

Source: Internet
Author: User
Some important differences between Ruby language and C # are:
1.ruby is a dynamic language, C # is a static language--that is, after the object is new, Ruby can also dynamically add some properties or methods to the object instance (JavaScript is the same)
2.ruby deliberately weakens the concept of variable type, by default, the variable/method does not need to declare a specific (return) type, but in fact, within Ruby, the value of the variable is automatically assigned to the type. (Can be viewed through the puts variable. Class)
3.ruby relative to C #, there may be some thunder in the place: the private members of the parent class, incredibly can be used in subclasses!
... Other places, and then write it when you've studied.
The last section of the test code shows the basic usage of classes in Ruby:
Copy Code code as follows:

Class people #跟javascript这类动态语言一样, and classes in Ruby do not have public,private such access control identifiers

def initialize (_name) #构造函数, name is fixed: Initialize
@name = _name; #约定: The private variable of a class begins with a @
End

def to_string #类似C # in the practice of writing a ruby version of the ToString method here
The "My name is #{@name}" #ruby中方法最后一行的值 and is returned as a function value
End

def get_name_size
return @name. Length #这个方法中返回的是数字型 (that is, the length of the name)
End

def Test #类定义的最后部分, Mark this method as a private method.
Puts "private method (test) in people."
End

def show_name
Test #私有方法, which can be called internally
Puts "name = #{@name}"
End

Attr_accessor:name #定义一个可读写的属性, it also embodies one of the important ideas of Ruby: the convention is more than the specification, because the @name has been used before, the attributes here need to be removed, and Ruby will automatically generate a similar set{in C # Value = Statement @name},get{return @name}

Private:test #标明test方法是private属性的
Protected:get_name_size #标明get_name_size只能在本类或子类定义中使用 (or assign to a subclass instance in a subclass definition)

End
Apeople = People.new ("Jimmy"); #创建一个People的实例
Puts Apeople.to_string #调用to_string方法
#puts Apeople.get_name_size #将报错 Because the method is protected
#puts Apeople.test #将报错, because the method is a private method
Apeople.show_name
Puts Apeople.name
Apeople.name = "Yang Junming" #修改姓名
Apeople.show_name
#再定义一个子类
Class Man < people
def initialize (_name)
Super
@sex = True
End

Attr_reader:sex #定义只读属性sex

def Call_protected_method
Puts Get_name_size #调用父类的受保护方法
End

def call_protected_method2 (Man1)
Puts Man1.get_name_size #注意这里: Here you can add the protected method of the parent class to the subclass instance dynamically
End

DEF call_private_method #子类可以调用父类的私有方法!!! This is just beginning to be very unaccustomed
Test
End

def call_private_method2 (Man1)
Man1.test #注意这里: The grammar check can pass, but the runtime prompts the private method not to call, which is the difference between private and protected
End


End
Puts "******************************"
AMan = Man.new ("Jimmy.yang");
Aman.show_name
Aman.call_protected_method
Puts Aman.sex
Aman.call_private_method
AMan2 = Man.new ("Mike")
AMAN.CALL_PROTECTED_METHOD2 (AMAN2);
#aMan. CALL_PRIVATE_METHOD2 (aMan2);
A = "abc";
#aMan. CALL_PROTECTED_METHOD2 (a); #虽然ruby本身对变量没有类型概念, but that doesn't work, that is, when you call a protected method of a parent class, you actually want the type to match
Puts Aman.class #显示aMan的类名称

The results of the operation are as follows:
Copy Code code as follows:

>ruby classdemo.rb
My name is Jimmy
Private method (test) in people.
Name = Jimmy
Jimmy
Private method (test) in people.
Name = Yang Junming
******************************
Private method (test) in people.
Name = Jimmy.yang
10
True
Private method (test) in people.
4
Mans
>exit code:0

The young under the Banyan tree
Source: http://yjmyzz.cnblogs.com
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.