ruby 新建对象

Before getting into understanding how Object-oriented programming is implemented in Ruby, let us first understand what Object Oriented means.

在了解如何在Ruby中实现面向对象的编程之前,让我们首先了解面向对象的含义。

Object-oriented programming reduces the complexity of large software systems thus making the software easier to maintain. This category of programming basically uses Objects. In a pure object-oriented language, everything is considered as an Object. The main aim of OOP is to combine the data and functions which are operating those data so that the data cannot be accessed by any other part of the code.

面向对象的编程降低了大型软件系统的复杂性,从而使软件易于维护。 这类编程基本上使用对象。 在纯面向对象的语言中,所有内容都被视为对象。 OOP的主要目的是将数据和操作这些数据的功能组合在一起,以使代码的任何其他部分都无法访问该数据。

Now let us talk about Ruby. Ruby is a pure Object oriented language and everything is considered as an object. Even Strings, Numbers, true or false is an Object being the most primitive type. Rest of the article will let you know, how classes and objects are implemented in Ruby?

现在让我们谈谈Ruby。 Ruby是一种纯面向对象的语言 ,所有内容都被视为对象。 甚至字符串,数字,true或false也是最原始的对象类型。 本文的其余部分将让您知道,如何在Ruby中实现类和对象?

类 (Class)

Class is nothing but a blueprint of a data type. It simply defines, what an instance of the class consists and possible functions which can be performed on the object of the class. In Ruby, even classes are objects of "class" class.

类不过是数据类型的蓝图。 它仅定义类的实例由什么组成,以及可以对类的对象执行的可能功能。 在Ruby中,甚至类都是“类”类的对象。

Syntax:

句法:

    class Class_name
#code
end

Remember that, the class name must start with a capital letter by convention. You will get an error at Compile time when the convention is not followed.

请记住,按照惯例,类名必须以大写字母开头。 如果不遵守约定,则会在编译时出现错误。

Now, let us declare a class in Ruby,

现在,让我们在Ruby中声明一个类,

    class Example
def initialize
end
def prints
end
end

The above code will be compiled but not yield an output because no memory is provided to the class until it is not instantiated.

上面的代码将被编译,但不会产生输出,因为在未实例化该类之前,不会为该类提供任何内存。

对象 (Objects)

Objects are the instance of a class. They provide memory to the class. You can create any number of objects of a class. The objects are created with the help of the "new" keyword.

对象是类的实例。 它们为班级提供记忆。 您可以创建任何数量的类的对象。 在“ new”关键字的帮助下创建对象。

Syntax:

句法:

    object_name = class_name.new

Let us understand object creation with the help of an example:

让我们借助示例来了解对象创建:

class Example
def initialize
end
def prints
puts "Hello fella. How are you!!"
end
end
ob1 = Example.new
ob1.prints
ob2 = Example.new
ob2.prints

Output

输出量

Hello fella. How are you!!
Hello fella. How are you!!

In the above code, you can observe that we are creating two objects of class Example. Then we are invoking prints method with the instances.

在上面的代码中,您可以观察到我们正在创建类Example的两个对象。 然后,我们将实例调用prints方法。

建设者 (Constructors)

Constructors are used to initialize the variable of a class. They initialize class variables at the time of object creation. ‘initialize’ method works as a constructor in Ruby. It is defined inside the class and is invoked with the creation of an object. Go through the syntax and example for a better understanding.

构造函数用于初始化类的变量。 它们在创建对象时初始化类变量。 'initialize'方法在Ruby中充当构造函数。 它在类内部定义,并在创建对象时调用。 通过语法和示例可以更好地理解。

Syntax:

句法:

    class Class_name
def initialize(parameters if required)
end
end

Example:

例:

=begin
Ruby program to demonstrate initialize.
=end
class Example
def initialize(j,k)
@a = k
@b = j
end
def prints
puts "The value of class variables are #{@a} and #{@b}"
end
end
ob1 = Example.new(2,5)
ob1.prints
ob2 = Example.new(9,7)
ob2.prints

Output

输出量

The value of class variables are 5 and 2
The value of class variables are 7 and 9

翻译自: https://www.includehelp.com/ruby/object-oriented-programming.aspx

ruby 新建对象

ruby 新建对象_Ruby中的面向对象编程相关推荐

  1. ruby 新建对象_Ruby面向对象编程的简介

    ruby 新建对象 by Saul Costa 由Saul Costa Object-oriented programming (OOP) is a programming paradigm orga ...

  2. ruby 新建对象_Ruby面向对象编程简介

    ruby 新建对象 by Nishant Mishra 由Nishant Mishra Ruby面向对象编程简介 (An Introduction to Object-Oriented Program ...

  3. python如何初始化对象数组_如何理解Python中的面向对象编程?

    (由Python大本营付费下载自视觉中国) 作者 | Radek Fabisiak 译者 | 弯月,责编 | 郭芮 出品 | CSDN(ID:CSDNnews) 现如今面向对象编程的使用非常广泛,本文 ...

  4. JS中的面向对象编程

    JS中的面向对象编程 小课堂 目录 1.背景介绍 2.知识剖析 3.常见问题 4.解决方案 5.编码实战 6.扩展思考 7.参考文献 8.更多讨论 1.背景介绍 什么是对象? ECMA-262把对象定 ...

  5. VSCode自定义代码片段9——JS中的面向对象编程

    JavaScript的面向对象编程 {// JS'OOP// 9 如何自定义用户代码片段:VSCode =>左下角设置 =>用户代码片段 =>新建全局代码片段文件... =>自 ...

  6. python采用面向对象编程模式吗_如何理解 Python 中的面向对象编程?

    现如今面向对象编程的使用非常广泛,本文我们就来探讨一下Python中的面向对象编程. 作者 | Radek Fabisiak 译者 | 弯月,责编 | 郭芮 以下为译文: Python支持多种类型的编 ...

  7. 如何理解 Python 中的面向对象编程?

    现如今面向对象编程的使用非常广泛,本文我们就来探讨一下Python中的面向对象编程. 作者 | Radek Fabisiak 译者 | 弯月,责编 | 郭芮 出品 | CSDN(ID:CSDNnews ...

  8. 第八章:对象、类与面向对象编程

    第八章:对象.类与面向对象编程 8.1 理解对象 new一个对象的时候发生了什么 8.1.1 属性的类型 通过两个中括号访问内部特性 1. 数据属性 数据属性包含一个保存数据值的位置 有 4 个特性描 ...

  9. python中对象的概念是什么_简述Python中的面向对象编程的概念

    面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机 ...

最新文章

  1. Python Flask学习知识点(七)
  2. UE4用音乐音效,各种声音都有,比较全了
  3. 数据挖掘概念与技术12--数据立方体的计算和多路数组聚集详解
  4. linux 分割pdf,PDFBox分割PDF文档
  5. eclipemaven本地仓库依赖_【Maven】解决本地jar依赖
  6. 2.移植uboot-添加2440单板,并实现NOR、NAND启动
  7. 求解斐波那契第n项的几种解法(含矩阵乘法+快速幂) Python实现
  8. python销毁线程_聊聊 Python 中的线程
  9. php公众号推荐,良心推荐6个优质实用又有趣的微信公众号!
  10. 在建工地扬尘在线监控系统推荐_关于印发《建设工地扬尘在线监控管理平台建设工作方案》的通知...
  11. 打开SVN server图形化管理界面
  12. Note8 android 9 root,三星Note8 国行 root N9500 9.0 root N9500ZCS6DTC1 root
  13. 漫威系列电影、电视剧的推荐观看顺序是怎样的?
  14. Maven报:Unable to import maven project: See logs for details
  15. java520.1314表白_数学学霸表白方程式520.1314
  16. excel图片 html,微商相册生成EXCEL表格中将原图按HTML格式显示的实例
  17. (七)苏世民:我的经验和教训:决策(34)
  18. 云主机装黑果实践(6):处理云主机上变色龙启动后置过程:驱动和黑屏
  19. 音频单元组件服务参考(Audio Unit Component Services Reference)
  20. 我在大学所构建的技术体系

热门文章

  1. 依赖 netty spring_十分钟带你了解Spring的七大知识点,程序员必了解
  2. 后台使用orm多还是直接sql_Django应用app创建及ORM
  3. ANSIBLE---变量
  4. go设计模式之单例模式
  5. 存储芯片在智能化产业链中扮演的角色将更加重要
  6. 创建SQL函数计算员工加班时间
  7. 俞永福卸任阿里大文娱董事长,改任 eWTP 投资组长
  8. 2017软件工程实践
  9. AP 1532E register   Cisco 2504 AP注册WLC
  10. android 底部菜单