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: 可以被 ...
随机推荐
- double转换long的疑问
在lua(5.1.4)下面测试的时候使用0x100000000的时候出现了问题,打印结果很明显,如下所示: Lua Copyright (C) - Lua.org, PUC-Rio > prin ...
- Linux:配置HTTPS,获取ssl证书
这里小仓鼠将Https配置流程记录下来 1.访问网站:https://www.aliyun.com/ss/?k=ssl 2.选择‘立即购买’ 3. 页面变换为: 4.点击‘立即购买’ 5.进行支付 6 ...
- c++ nested class 嵌套类。
c++ primer 658页 嵌套类最常用于定义执行类,
- MSSQL注入常用SQL语句整理
很多情况下使用工具对mssql注入并不完善,所以我们就需要手工注入,一下是本人收集的一些mssql的sql语句. 手工MSSQL注入常用SQL语句 and exists (select * from ...
- [转]How to Leak a Context: Handlers & Inner Classes
Consider the following code: public class SampleActivity extends Activity { private final Handler mL ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- 怎么解决深入学习PHP的瓶颈?
PHP给学习者的感觉是:初学的时候很容易,但是学了2-3年,就深刻感觉遇到了瓶颈,很难深入,放弃又可惜.所谓“鸡肋,食之无味弃之可惜”的感觉很是贴切. 经常会有这种感觉:不学,看似也不后退:学了,好像 ...
- 3springboot:springboot配置文件(配置文件、YAML、属性文件值注入<@Value、@ConfigurationProperties、@PropertySource,@ImportResource、@Bean>)
1.配置文件: springboot默认使用一个全局配置文件 配置文件名是固定的 配置文件有两种(开头均是application,主要是文件的后缀): ->application.prope ...
- .net mvc 路由
Asp.net Mvc之Action如何传多个参数 在Global.asax文件中,默认路由如下. routes.MapRoute( "Default", // 路由名称 &quo ...
- POJ 1328 Radar Installation(很新颖的贪心,区间贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 106491 Accepted: 2 ...