Attribute is a very useful propety in Ruby.It like the 'get/set' method in Java and the 'property' in C#,but it is easier and more useful than these language.For example,in Java language you need write too codes like these:
class A{
private int a;
public int getA(){
return a;
}
public void setA(int a){
this.a = a;
}
....
}
And you see,there are too many codes.In C#,it may be easyer than java,but you need write more code,too.
In Ruby,you can write 'get method' codes like the following:
attr_reader :name
then,if there are many instance variables which need be read,you can write following:
attr_reader :name,:artist,:duration
Use ',' to separate these variables(Note:remember that there is a space between the 'reader' word and the ':').
The 'set' method is like
attr_writer :name,:artist,:duration
And the 'get/set' method:
attr_accessor :name,:artist,:duration
Now,you can see why I say that the Attribute in Ruby is easier.And you will know why I say that the Attribute in Ruby is more useful in the next part which I reference from
<pragmatic.programmers.programming.ruby.2nd>:
These attribute-accessing methods do not have to be just simple wrappers around an
object¡¯s instance variables. For example, you may want to access the duration in minutes
and fractions of a minute, rather than in seconds as we¡¯ve been doing.
</pragmatic.programmers.programming.ruby.2nd>
ruby 代码
- class Song
- def duration_in_minutes
- @duration/60.0
- end
- def duration_in_minutes=(new_duration)
- @duration = (new_duration*60).to_i
- end
- end
- song = Song.new("Bicylops", "Fleck", 260)
- song.duration_in_minutes ! 4.33333333333333
- song.duration_in_minutes = 4.2
- song.duration ! 252
Here we¡¯ve used attribute methods to create a virtual instance variable. To the outside
world, duration_in_minutes seems to be an attribute like any other. Internally,
though, it has no corresponding instance variable.
This is more than a curiosity. In his landmark book Object-Oriented Software Construction
[Mey97], Bertrand Meyer calls this the Uniform Access Principle. By hiding
the difference between instance variables and calculated values, you are shielding the
rest of the world from the implementation of your class. You¡¯re free to change how
things work in the future without impacting the millions of lines of code that use your
class. This is a big win.
Note:there is a '=' when you define the
Virtual Attribute between the name of attribute and the argument
ruby 代码
- def duration_in_minutes=(new_duration)
分享到:
相关推荐
Predicting face attributes in the wild is challenging due to complex face variations. We propose a novel deep learning framework for attribute prediction in the wild. It cascades two CNNs, LNet and ...
On the handling of Continuous-valued attributes in decisiontree generation
在分类及预测任务中对高维类别(category)变量的预处理方法
Ruby对象属性实验室 目标 定义Ruby类。 定义读取和写入实例变量的方法。 使用方法和实例变量创建对象属性。 概述 本实验全部关于使用类中的实例变量来创建表示对象的属性或属性的方法。 指示 通过打开“ learn ...
commons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zip
RADIUS Attributes
Attributes结构的一个总结,适合java开发者使用。
刘轶-BDTC-Extended Attributes and Transparent Encryption in Hadoop1.zip
Laravel开发-attributes Rinvex属性是一种健壮、智能和集成的实体属性值模型(EAV)实现,用于Laravel雄辩,具有强大的底层功能,可以轻松地将实体属性隐式管理为关系。它利用了Laravel雄辩的力量,平滑无缝的集成。
CSharp - Module 14_Attributes
Attributes are used to modify the runtime behavior of code in the .NET Framework. Applied .NET Attributes explores the application of .NET attributes and how developers can write custom attributes ...
Verifying specifications with associated attributes in graph transformation systems
JavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-...
commons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zip
commons-attributes-api.jar,commons-attributes-compiler.jar,commons-beanutils.jar,commons-codec.jar,commons-collections.jar,commons-dbcp.jar,commons-digester.jar,commons-discovery.jar,commons-...
一个对XmlDocument的DocumentElement 以及其属性Attributes进行访问的类库函数。
org-gephi-data-attributes-api可视化软件gephi的插件,nbm格式,
tkinter的menu attributes示例代码,结合https://blog.csdn.net/weixin_43708622/article/details/107053182一起学习
commons-attributes-api.jar
介绍了Adaptive Color Attributes for Real-Time Visual Tracking,CVPR2014,文章中算法的基本流程,以及优化的重点