类中的public,protect,private

public method

class Point
def test
end
end

这样定义的test方法就是一个public方法可以在类内外使用

protected method

protected

protected 权限的函数只能在被本类或子类的上下文中调用,单可以使用other_object.function的形式。这个关键是可以调用本类的其他对象的protected函数

class Point
def test
end
protected :test
end

或者

class Point
protected
def test
end
end

实例:

 # Now make 'test' protect

  class B
protected :test_access
end p A.new.test_access # 错误 private method 'test_access'
B.new.test # test access right p B.new.test_other(B.new) # test access right p B.new.test_other(A.new) # 错误 private method 'test_access'

只要在protected关键词之后那么就是protected

private

只能在本对象的实例中访问本对象的private方法,Ruby中private意为不能指定方法接收者,接收者只能是self,且self必须省略;

private权限的函数只能在本对象成员函数中访问到。也就是这个函数只是对这个对象的实现细节。

class A
private
def test_access
@instance_attribute = "instance_attribute"
return "test access right"
end
end class B < A
def test
puts test_access
puts @instance_attribute
end def test_other(a)
a.test.access
end
end -------------------------
B.new.test # => test access right
# = > instance_attribute A.new.test_access #错误test.rb:20: private method `test_access' called for #<A:0x3fd9064> (NoMethodError)
B.new.test_other(A.new) #错误test.rb:18: in `test_other': private method `test_access' called for #<A:0x4198850> (NoMethodError) from test.rb:24

基本继承

直接进入主题吧,我们先定义一个类

class Point
def initialize(x, y)
# @x instance variable
# @@x class variable
# $x global variable
# x local variable
@x, @y = x, y
end def first_quadrant?
x > 0 && y >0
end
end

这里我们创建一个新的类继承原来的类

class Point3D << Point
def initialize(x, y, z)
@x, @y, @z = x, y, z
end
end

但是我们可以使用super方法

class Point3D << Point
def initialize(x, y, z)
super(x, y)
@z = z
end
end

继承了父类中的方法,不用写重复的代码了

Ruby中类的进阶(继承,private, public, protect)的更多相关文章

  1. c++三种继承方式public,protect,private

    C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...

  2. public protect private. 草稿。

    public protect private. 草稿. #include <iostream> #include <thread> #include <memory> ...

  3. Java中public、private、protect对数据成员或成员函数的访问限制

    Java类中对数据成员.成员函数的访问限制修饰有:public.protect.private.friendly(包访问限制) public修饰的数据成员或成员函数是对所有用户开放的,所有用户可以直接 ...

  4. C++:private继承与public继承

    1 private, public, protected 访问标号的访问范围 private:只能由1.该类中的函数.2.其友元函数访问. 不能被任何其他访问,该类的对象也不能访问. protecte ...

  5. java中public与private还有protect的区别

    java中public与private还有protect的区别 总是忘记.

  6. c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承

    公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...

  7. public,protect,private访问权限

    第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2.其友元函数访问.不能被任何其他访问,该类的对象也不能访问. protect ...

  8. 第二十八节:Java基础-进阶继承,抽象类,接口

    前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...

  9. C++ private,public,protected 关键字

    第一: private,public,protected的访问范围:   private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. protected: 可以被 ...

随机推荐

  1. 【Leetcode】【Medium】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  2. QT控件大小的方法

    http://blog.csdn.net/liang19890820/article/details/51986284

  3. IEEP部署企业级网络工程-网络故障-环路故障

    网络故障 1.环路故障 概念 1).以太网是一个支持广播的网络, 在没有环路的环境中,广播报文在网络中以泛洪的形式被送达到网络的第一个角落,以保证每个设备都能够接受到它.每台二层设备在接收到广播报文以 ...

  4. ppt中调整图片位置

    按方向键时,如果调整的位置过大,可以使用 Ctrl + 方向键.

  5. 32位 64位 获得进程peb的方法

    基于上一篇文章,大概了解了peb的获取方法,但是那个方法只能获得当前进程的PEB,不能获得其他的进程的PEB.根据那个思想,获得其他进程PEB则需要注入,得到进程信息,然后进程间通信,将信息返回来,经 ...

  6. 一个SAP开发人员的2018年终总结

    我是SAP成都研究院的Jerry Wang,我喂自己袋盐. 时间过得真快,2017年发生的事情还历历在目,一转眼,2018年又马上要结束了. Jerry惊恐地发现,随着年龄的增长,时光流逝的速度仿佛有 ...

  7. [零基础学JAVA]Java SE基础部分-02.标识符、数据类型

    转自:http://redking.blog.51cto.com/27212/114976 1.课程名称:标识符.数据类型 本季介绍了Java中的标识符的命名规则,各种关键字及数据类型的划分,并对各种 ...

  8. PTA练习题之7-2 求交错序列前N项和(15 分)

    本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在一行中输出部分和的值,结果保留三位小数. ...

  9. Ios国际化翻译工具

    IOS Translation Tool(IOS国际化翻译工具) 介绍 当IOS项目国际化的时候,手工去翻译每一个字符串是一件非常痛苦的事情.尤其是当项目中存在N多种语言.而且又很难保证,手工翻译的准 ...

  10. Windows Server 2008 安装wampserver失败的总结

    1.最好选择2.4以下版本. 2.如果报确实MSVCR100.dll的错误,需要下载运行环境包(参考博客)