S3 系统是宽泛且灵活的,同类的 S3 对象也可能有不同的成员。但是,对于 S4 系统,
就不会发生,也就是说,当我们创建一个属于某类的 S4 对象实例时,不能任意添加不在类
表示中的字段。
举个例子,在创建一个新的 Product 类的对象实例时,我们不能添加 volume 字段:
bottle <- new("Product", name = "Bottle",
price = 3, inventory = 100L, volume = 15)
## Error in initialize(value, ...): invalid name for slot of class "Product":
volume
相反,添加只能通过合适的继承来完成。我们需要创建一个新类,它包含(或继承自)
原始类。在这个例子中,我们定义一个 Container 类,它继承 Product 类,而且有一个
新的名为 volume 的数值字段:
setClass("Container",
representation(volume = "numeric"),
contains = "Product")
因为 Container 类继承 Product 类,所以任何 Container 类的对象实例都有
Product 类的所有字段,可以使用 getSlots( )进行查看:
getSlots("Container")
## volume name price inventory
## "numeric" "character" "numeric" "integer"
现在,我们创建一个含有 volume 字段的 Container 类对象实例:
bottle <- new("Container", name ="Bottle",
price = 3, inventory = 100L, volume = 15)
要注意,在创建 Container 类的对象实例时,Product 类的验证函数仍起作用:
bottle <- new("Container", name ="Bottle",
price = 3, inventory = -10L, volume = 15)
## Error in validObject(.Object): invalid class "Container" object: inventory
must be non-negative
因此,执行检查可以确保 Product 类字段的有效性,但是检查对 Container 类独有
的字段不起作用:
bottle <-new("Container", name = "Bottle",
price = 3, inventory = 100L, volume = -2)
我们为 Product 类定义了验证函数,同样,也可以为 Container 类定义一个验证函数:
validate_container <- function(object) {
errors <- c(
if (length(object@volume) != 1)
"Length of volume must be 1",
if (object@volume <= 0)
"volume must be positive"
)
if (length(errors) == 0) TRUE else errors
}
这样,我们就可以使用这个验证函数重新定义 Container 类:
setClass("Container",
representation(volume = "numeric"),
contains ="Product",
validity = validate_container)
注意到,不需要在validate_container( )中调用validate_product( ),因为两
个验证函数会被依次调用来确保所有位于继承链上的类都被(自己的验证函数)适当地检查了。
当然,也可以在验证函数中添加一些文本打印代码,以此确保在创建一个Container 类的对象
实例时,validate_product( )总是比validate_container( )先被调用:
bottle <- new("Container", name = "Bottle",
price = 3, inventory = 100L, volume = -2)
## Error in validObject(.Object): invalid class "Container" object: volume
must be positive
bottle <- new("Container", name = "Bottle",
price = 3, inventory = -5L, volume = 10)
## Error in validObject(.Object): invalid class "Container" object:
inventory must be non-negative

S4 继承的更多相关文章

  1. S4 对象系统

    上一节中,我们介绍了 S3 系统.与大多数其他编程语言的面向对象系统不同,与那些类被定义为固定结构,且随着程序编译有确定的方法分派的系统相比,S3 系统显得非常不严谨.当我们定义一个 S3 类时,几乎 ...

  2. Alignment And Compiler Error C2719 字节对齐和编译错误C2719

    Compiler Error C2719 'parameter': formal parameter with __declspec(align('#')) won't be aligned The ...

  3. JavaSE基础:泛型

    泛型 1.引入 情景模式描述,假设完成一个学生的成绩的情况: 整数: math=80,english=70 小数: math=85.6,englisth=77.8 字符串: math="66 ...

  4. 【一次面试】再谈javascript中的继承

    前言 面向对象编程是每次面试必问的知识点,而前端js如何实现继承每次命中率高达80% 这不,近两天我们面试时候,同事就问道面试者此问题,但是,不论之前自己做的回答,还是面试者的回答,基本都不太令人满意 ...

  5. 理解C#中的继承

    继承 1.创建子类对象的时候,在子类对象中会为子类对象的字段开辟空间,也会为父类对象的所有字段开辟空间.只不过父类私有的成员访问不到而已. 2.base关键字可以调用父类的非私有成员. 3.子类的访问 ...

  6. 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

    描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...

  7. C++中的继承详解(3)作用域与重定义,赋值兼容规则

    作用域与同名隐藏 一样的,先上代码 1 class A 2 { 3 public: 4 int a_data; 5 void a() 6 { 7 cout << "A" ...

  8. C++中的继承(3)作用域与重定义,赋值兼容规则

    作用域与重定义(同名隐藏) 一样的,先上代码 1 class A 2 { 3 public: 4 int a_data; 5 void a() 6 { 7 cout << "A& ...

  9. 详谈Javascript类与继承

    本文将从以下几方面介绍类与继承 类的声明与实例化 如何实现继承 继承的几种方式 类的声明与实例化 类的声明一般有两种方式 //类的声明 var Animal = function () { this. ...

随机推荐

  1. Android自动读取短信验证码

    Android自动读取短信验证码  extends:http://www.cnblogs.com/jiayaguang/p/4366384.html,http://blog.csdn.net/yung ...

  2. jdbc将数据库连接信息放置配置文件中

    目录如下: jdbcConnection.java: package jdbc01; import java.io.InputStream; import java.sql.Connection; i ...

  3. c# 调用声音文件

    一.使用C#自带的SoundPlayer using System.Media; SoundPlayer sound = new SoundPlayer("声音.wav"); so ...

  4. Optimal Milking---poj2112(多重匹配+Floyd+二分)

    题目链接:http://poj.org/problem?id=2112 题意:K个挤奶器(编号1~K),每个挤奶器每天最多供M头奶牛.共有C头奶牛(编号K+1~K+C).挤奶器和奶牛间有不同长度的路. ...

  5. mysql 数据操作 单表查询 where约束 练习

    create table employee( id int not null unique auto_increment, name ) not null, sex enum('male','fema ...

  6. IDEA阿里巴巴Java开发规约插件使用

    前言 2017年2月9日,首次公布<阿里巴巴Java开发手册>; 2017年9月25日,阿里巴巴集团发布了<阿里巴巴Java开发手册>PDF终极版; 2017年10月14日,在 ...

  7. Deep Learning(3)算法简介

    查看最新论文 Yoshua Bengio, Learning Deep Architectures for AI, Foundations and Trends in Machine Learning ...

  8. layer官方演示与讲解(jQuery弹出层插件)

    1. 使用layer遇到困难?Fly社区虔诚为您解惑 2. layer 2.0 发布,以独立形式呈现的最后一个版本 3. Fork layer on Github,爱她,就给她加个星啵 当前版本:2. ...

  9. h5前端项目常见问题汇总

    原文作者:FrontEndZQ 原文链接:https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度 ...

  10. vim7.4在Win8下的安装及简单配置

    软件环境 vim74 git vim中文帮助 vundle安装——插件管理软件 cd vim所在路径/vimfiles/bundle git clone https://github.com/gmar ...