Ruby是一种面向对象编程语言,这意味着它操纵的编程结构称为"对象"

先上代码, 了解类的定义与使用方式

class Computer
$manufacturer = "Mango Computer, Inc."
@@files = {hello: "Hello, world!"} def initialize(username, password)
@username = username
@password = password
end def current_user
@username
end def self.display_files
@@files
end
end # Make a new Computer instance:
hal = Computer.new("Dave", 12345) puts "Current user: #{hal.current_user}"
# @username belongs to the hal instance. puts "Manufacturer: #{$manufacturer}"
# $manufacturer is global! We can get it directly. puts "Files: #{Computer.display_files}"
# @@files belongs to the Computer class.
---------------------------------------------------------------------
输出:
Current user: Dave
Manufacturer: Mango Computer, Inc.
Files: {:hello=>"Hello, world!"}
nil

类的定义

class Computer
  #class magic here
end

根据Ruby命名约定, 类的名称第一个字母要大写,之后每个单词的首字母大写, 两个单词之间不再用下划线_分隔

class Computer
def initialize end
end

观察上面类的定义, 类中出现了initialize这个方法, 这个方法用来初始化类的对象,如果没用这个方法,对象就无法生成对象。(对应于c++/java的构造函数)

class Computer
$manufacturer = "Mango Computer, Inc."
@@files = {hello: "Hello, world!"} def initialize(username, password)
@username = username
@password = password
end
end

在Ruby的世界里, 我们用在一个变量名前加上@表示这个变量是一个实例变量,这意味着这个变量只能被类的实例所访问。

局部变量只作用于某个具体的方法中。

变量名前有两个@, 即@@的变量,叫做类变量,它并不属于类的实例,而是属于类自身(类似C++中的static变量)。

全局变量有两种定义方式, 一种是定义在所有类和方法之外。如果你想把全局变量放入类中, 那么采用另一种定义方式,在变量名之间加上$。

Ruby类的创建与使用的更多相关文章

  1. Ruby 类的创建

    class Language  def initialize(name, creator) @name = name @creator = creator end def description pu ...

  2. Ruby类

    Ruby类 类定义 #!/usr/bin/ruby class Sample def hello puts "Hello Ruby!" end end # 使用上面的类来创建对象 ...

  3. 雷林鹏分享:Ruby 类和对象

    Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...

  4. 雷林鹏分享:Ruby 类案例

    Ruby 类案例 下面将创建一个名为 Customer 的 Ruby 类,您将声明两个方法: display_details:该方法用于显示客户的详细信息. total_no_of_customers ...

  5. Ruby 类和对象

    Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...

  6. C# 根据类名称创建类示例

    //获得类所在的程序集名称(此处我选择当前程序集) string bllName = System.IO.Path.GetFileNameWithoutExtension(System.Reflect ...

  7. php简单实用的操作文件工具类(创建、移动、复制、删除)

    php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) {  // 原目录,复制到的目录 $dir = opend ...

  8. 李洪强iOS开发之OC[013] -类的创建的练习

    // //  main.m //  12 - 类的创建练习 // //  Created by vic fan on 16/7/9. //  Copyright © 2016年 李洪强. All ri ...

  9. C++:类的创建

    类的创建 #include<iostream> #include<cmath> using namespace std; class Complex //声明一个名为Compl ...

随机推荐

  1. Zabbix网络自动发现规则和自动添加hosts及link模板

    Version: zabbix 3.0 一.配置网络发现规则 Device uniqueness criteria:选择主机名作为唯一标识(Configuation Hosts中显示的NAME) 二. ...

  2. Linux 中强大且常用命令:find、grep

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下:    find命令        f ...

  3. runtime笔记一

    一.iOS中_cmd The _cmd variable is a hidden argument passed to every method that is the current selecto ...

  4. dotnetbar入门

    1.下载dotnetbar组件 2.工具箱引用 3.项目引用 4.开始工作 //此处Form完整的名称是System.Windows.Forms.Form,表示FrmMain窗体类是继承于System ...

  5. Java Map 简介

    AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, H ...

  6. 安装Flask

    安装Flask步骤 输入网址https://bitbucket.org/pypa/setuptools](https://bitbucket.org/pypa/setuptools,回车后进入setu ...

  7. C#读取Excel,DataTable取值为空的解决办法

    连接字符串这么些就行了 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + opnFileName ...

  8. 如何用Nsight调试C# OpenGL程序

    https://devtalk.nvidia.com/default/topic/804306/nsight-visual-studio-edition/nsight-4-5-can-t-debug- ...

  9. WKWebView使用及注意点

    iOS8之后,苹果推出了WebKit这个框架,用来替换原有的UIWebView,新的控件优点多多,不一一叙述.由于一直在适配iOS7,就没有去替换,现在仍掉了iOS7,以为很简单的就替换过来了,然而在 ...

  10. GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...