ruby学习笔记系列(4)

Access Control

When designing a class interface, it’s important to consider just how much access to
your class you’ll be exposing to the outside world. Allow too much access into your
class, and you risk increasing the coupling in your application—users of your class will
be tempted to rely on details of your class’s implementation, rather than on its logical
interface. The good news is that the only easy way to change an object’s state in Ruby
is by calling one of its methods. Control access to the methods, and you’ve controlled
access to the object. A good rule of thumb is never to expose methods that could leave
an object in an invalid state. Ruby gives you three levels of protection.
• Public methods can be called by anyone—no access control is enforced.Methods
are public by default (except for initialize, which is always private).
• Protected methods can be invoked only by objects of the defining class and its
subclasses. Access is kept within the family.
• Privatemethods cannot be called with an explicit receiver—the receiver is always
self. This means that private methods can be called only in the context of the
current object; you can’t invoke another object’s private methods.
The difference between “protected” and “private” is fairly subtle and is different in
Ruby than in most common OO languages. If a method is protected, it may be called
by any instance of the defining class or its subclasses. If a method is private, it may
be called only within the context of the calling object—it is never possible to access
another object’s private methods directly, even if the object is of the same class as the
caller.

Ruby differs from other OO languages in another important way. Access control is
determined dynamically, as the program runs, not statically. You will get an access
violation only when the code attempts to execute the restricted method.
Specifying Access Control
You specify access levels to methods within class or module definitions using one or
more of the three functions public, protected, and private. You can use each function
in two different ways.
If used with no arguments, the three functions set the default access control of subsequently
defined methods. This is probably familiar behavior if you’re a C++ or Java
programmer, where you’d use keywords such as public to achieve the same effect.
class MyClass
def method1 # default is 'public'
#...
end
protected # subsequent methods will be 'protected'
def method2 # will be 'protected'
#...
end
private # subsequent methods will be 'private'
def method3 # will be 'private'
#...
end
public # subsequent methods will be 'public'
def method4 # and this will be 'public'
#...
end
end
Alternatively, you can set access levels of named methods by listing them as arguments
to the access control functions.
class MyClass
def method1
end
# ... and so on
public :method1, :method4
protected :method2
private :method3
end
It’s time for some examples. Perhapswe’remodeling an accounting system where every
debit has a corresponding credit. Because we want to ensure that no one can break this
rule, we’ll make the methods that do the debits and credits private, and we’ll define our
external interface in terms of transactions.

class Accounts
def initialize(checking, savings)
@checking = checking
@savings = savings
end
private
def debit(account, amount)
account.balance =
amount
end
def credit(account, amount)
account.balance += amount
end
public
#...
def transfer_to_savings(amount)
debit(@checking, amount)
credit(@savings, amount)
end
#...
end
Protected access is used when objects need to access the internal state of other objects
of the same class. For example, we may want to allow individual Account objects to
compare their raw balances but may want to hide those balances from the rest of the
world (perhaps because we present them in a different form).
class Account
attr_reader :balance # accessor method 'balance'
protected :balance # and make it protected
def greater_balance_than(other)
return @balance > other.balance
end
end
Because the attribute balance is protected, it’s available only within Account objects.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值