0
Example of the problem - class.a has slots 1, 2, 3 
and inherits from class c (4, and 5).  
The slots in class (1, 2, 3) are passed to function.b as variables. 
the output of function b is as class.c.  So now I have the following.



class.a 
   slot 1  a value
   slot 2  a value
   slot 3  a value
   slot 4  empty
   slot 5  empty

   class.c
   slot 4 a result
   slot 5 a result

class.c 可以简单地合并到 class.a 中吗?如果是这样,怎么做?我搜索了文档,并且查看了虚拟和超类。我找不到关于将课程合并在一起的任何内容

这是创建类的代码 -

setClass("BondDetails",
       representation(
       Cusip = "character",
       ID = "character",
       BondType = "character",
       Coupon = "numeric",
       IssueDate = "character",
       DatedDate = "character",
       StartDate = "character",
       Maturity = "character",
       LastPmtDate = "character",
       NextPmtDate = "character",
       Moody = "character",
       SP = "character",
       BondLab  = "character",
       Frequency = "numeric",
       BondBasis = "character",
       Callable = "character",
       Putable = "character",
       SinkingFund = "character"))

setClass("BondCashFlows",
     representation(
     Price = "numeric",
     Acrrued = "numeric",
     YieldToMaturity = "numeric",
     ModDuration = "numeric",
     Convexity = "numeric",
     Period = "numeric",
     PmtDate = "Date",
     TimePeriod = "numeric",
     PrincipalOutstanding = "numeric",  
     CouponPmt = "numeric",
     TotalCashFlow = "numeric"))

 setClass("BondTermStructure",
     representation(
     EffDuration = "numeric",
     EffConvexity = "numeric",
     KeyRateTenor = "numeric",
     KeyRateDuration = "numeric",
     KeyRateConvexity = "numeric"))

 setClass("BondAnalytics",
     contains = c("BondDetails","BondCashFlows", "BondTermStructure"))

BondDetails 是存储的信息 BondCashFlows 是使用债券详细信息的输入计算的 BondTermStructure 是使用债券详细信息和bondcashflows 的输入计算的

我需要将它们全部放入 BondAnalytics 中,这样我才能创建一种输出方法,但我似乎无法正确理解它们是如何变成超类的

4

1 回答 1

1

取决于其他 700 行代码的作用……默认情况下initialize,S4 类的方法是一个复制构造函数,将未命名的参数作为基类的实例。也许你的意思是你有类似的东西(我的 A、B、C 类与你的类不对应,但不知何故,命名似乎对我理解你的问题更有意义)

.A <- setClass("A", representation(a="numeric"))
.B <- setClass("B", representation(b="numeric"))
.C <- setClass("C", contains=c("A", "B"))

.A(...)是“A”类等的自动生成的构造函数;.A(...)创建类“A”的新实例,然后initialize使用新实例和参数调用(即复制构造函数)...。一种可能性是你有一个“A”的实例和一个“B”的实例,并且你想构造一个“C”的实例

a <- .A(a=1)
b <- .B(b=2)

> .C(a, b)              # Construct "C" from base classes "A" and "B"
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2

另一种可能性是您已经有一个“C”实例,并且您想更新值以包含来自“B”实例的值

b <- .B(b=2)
c <- .C(a=1, b=3)

进而

> initialize(c, b)     # Copy c, replacing slots from 'B' with 'b'
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2
于 2013-09-30T00:52:57.570 回答