Ruby中类的进阶(继承,private, public, protect)
类中的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)的更多相关文章
- c++三种继承方式public,protect,private
C++中的三种继承public,protected,private 三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员 ...
- public protect private. 草稿。
public protect private. 草稿. #include <iostream> #include <thread> #include <memory> ...
- Java中public、private、protect对数据成员或成员函数的访问限制
Java类中对数据成员.成员函数的访问限制修饰有:public.protect.private.friendly(包访问限制) public修饰的数据成员或成员函数是对所有用户开放的,所有用户可以直接 ...
- C++:private继承与public继承
1 private, public, protected 访问标号的访问范围 private:只能由1.该类中的函数.2.其友元函数访问. 不能被任何其他访问,该类的对象也不能访问. protecte ...
- java中public与private还有protect的区别
java中public与private还有protect的区别 总是忘记.
- c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承
公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...
- public,protect,private访问权限
第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2.其友元函数访问.不能被任何其他访问,该类的对象也不能访问. protect ...
- 第二十八节:Java基础-进阶继承,抽象类,接口
前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...
- C++ private,public,protected 关键字
第一: private,public,protected的访问范围: private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. protected: 可以被 ...
随机推荐
- 解决因 RsFX devicer 而无法卸载 SQL Server 的问题
到卸载 SQL Server 2008 R2 安装程序支持文件时,遇到了问题:“警告 26003.无法卸载 Microsoft SQL Server 2008 R2 安装程序支持文件,因为安装了以下产 ...
- JS调用AngularJS中的方法
案例: 在IONIC中使用百度地图的JS版SDK,在该JS相关界面中需要触发点击事件来实现在Ionic的JS中定义的函数,参考方法如下: onclick="var $scope = angu ...
- window 服务注册、卸载
1.以管理员身份打开 命令窗口 2.服务注册命令:sc create myServer binpath= path 3.服务卸载命令:sc delete myServer Topshelf 服务安装 ...
- Task执行内幕与结果处理解密
本课主题 Task执行内幕与结果处理解密 引言 这一章我们主要关心的是 Task 是怎样被计算的以及结果是怎么被处理的 了解 Task 是怎样被计算的以及结果是怎么被处理的 Task 执行原理流程图 ...
- 学习Road map Part 01 数学
方法: 结合编程软件 matlab / octave / python / maxima / ruby 线性代数 向量.行列式 线性方程组 LU 分解 特征值.对角化 特征值算法
- July 25th 2017 Week 30th Tuesday
Everything is always more beautiful reflected in your eyes. 一切事物映在你的眼里都会变得更美. Looking in your eyes, ...
- 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创
马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴 ...
- 来自Google资深工程师的API设计最佳实践
来自Google资深工程师Joshua Bloch的分享:API设计最佳实践 为什么API设计如此重要?API是一个公司最重要的资产. 为什么API的设计对程序员如此重要? API一旦发布,出于兼容性 ...
- HTML DOM 初学笔记
JavaScript HTML DOM 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树,如图: 通过可编程的对象 ...
- Perl中的字符串操作函数
1.$position = index(string,substring,skipchars): 该函数返回子串substring在字符串string中的位置,如果不存在,则返回-1:参数skipch ...