ruby中argv_Ruby中的封装

ruby中argv

In this article, we will study about Encapsulation. We know that Ruby is a pure object-oriented language and in every object-oriented language, encapsulation is considered as one of the important properties. So, let us understand encapsulation with the help of program codes and syntaxes.

在本文中,我们将研究封装。 我们知道Ruby是一种纯粹的面向对象的语言,在每种面向对象的语言中,封装都被视为重要的属性之一。 因此,让我们借助程序代码和语法来了解封装。

Wrapping up the data into a single unit is known as Encapsulation. It can also be called as the procedure to bind code and the data which is affected by the code together. By applying encapsulation you can protect your data from being manipulated by another source. In simpler words, it can be considered as the mechanism which allows data only to be manipulated by the member functions of the class in which they are declared.

将数据打包为一个单元称为封装 。 它也可以称为将代码和受代码影响的数据绑定在一起的过程。 通过应用封装,可以保护您的数据免受其他来源的操纵。 用简单的话来说,它可以被视为一种机制,它仅允许数据由声明它们的类的成员函数来操纵。

如何实现封装? (How to achieve Encapsulation?)

You can achieve encapsulation by declaring all the variables of class as private( they are by default private) and all the member functions as public(they are by default public). Now, these variables will only be accessed by these public methods of the class.

您可以通过将class的所有变量声明为private(默认情况下为private)并将所有成员函数声明为public(默认情况下为public)来实现封装。 现在,这些变量只能由该类的这些公共方法访问。

封装的优势 (Advantages of Encapsulation)

  • Easy to test code: Unit testing is the testing that is known to every programmer. After achieving encapsulation, unit testing becomes easy to be done.

    易于测试的代码:单元测试是每个程序员都知道的测试。 完成封装后,单元测试变得容易完成。

  • Decreases Redundancy: Encapsulation helps with making the code reusable. You can update the code as per the requirement of time.

    减少冗余:封装有助于使代码可重用。 您可以根据时间要求更新代码。

  • Data Hiding: Encapsulation facilitates data hiding which means that user will not be able to get an idea about the internal implementation of the class. The user will be unaware of where the data is being stored in the class.

    数据隐藏:封装有助于数据隐藏,这意味着用户将无法获得有关类的内部实现的想法。 用户将不知道数据在类中的存储位置。

Let us understand encapsulation with the help of an example:

让我们借助示例来了解封装:

=begin
Ruby program to demonstrate encapsulation
=end
class Bank
	def initialize(id, nme, no, type)
		@cust_id = id
		@cust_name = nme
		@ac_no = no
		@cust_type = type
	end

	def display_details
		puts "Customer id : #{@cust_id}"
		puts "Customer name : #{@cust_name}"
		puts "Customer no : #{@ac_no}"
		puts "Customer type : #{@cust_type}"
	end
end

customer1 = Bank.new("Cust101", "Rashmeet", "AC789", "Savings")
customer2 = Bank.new("Cust102", "Parmeet", "AC1789", "Savings")
customer3 = Bank.new("Cust103", "Jagmeet", "AC2789", "Savings")
customer1.display_details
customer2.display_details
customer3.display_details

Output

输出量

Customer id : Cust101
Customer name : Rashmeet
Customer no : AC789
Customer type : Savings
Customer id : Cust102
Customer name : Parmeet
Customer no : AC1789
Customer type : Savings
Customer id : Cust103
Customer name : Jagmeet
Customer no : AC2789
Customer type : Savings

Explanation:

说明:

In the above code you can observe that the member functions and data are only accessible through the object or instance of the class. Here the instances of class "Bank" are customer1, customer2 and customer3.

在上面的代码中,您可以观察到成员函数和数据只能通过类的对象或实例进行访问。 在这里,“ 银行 ”类的实例是customer1customer2customer3

翻译自: https://www.includehelp.com/ruby/encapsulation.aspx

ruby中argv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值