2013-09-29 41 views
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中吗?如果是这样,怎么样?我搜查了文档和 我看了一个虚拟和超类。 我不能合并一起上课操作S4类

这里找到什么是创建类的代码 - 利用债券细节 BondTermStructure使用债券输入项计算输入的计算存储信息 BondCashFlows

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

我需要把它们都纳入BondAnalytics,所以我可以创建一个输出的方法,我只是 似乎无法得到它的权利这些如何变成了超类

+0

您可以重写函数b来获取类a的对象,将结果分配给插槽4和5,然后再次返回对象? –

+0

可以将类设置为“继承”,但我没有看到关于“合并”意味着能够回答此问题的充分解释概念。代码....我们需要代码。 –

+0

约有700行代码覆盖了三个函数,它们传递来自S4对象的变量。我不知道该软件甚至可以在这里发布。通过合并我的意思是说我有一个拥有命名插槽的超类,并且还从另外两个类继承。该软件运行其他功能并为其他类(不是超类)生成结果。所以,我认为这些类必须足够聪明才能插入正确的超类插槽。现在,我不这么认为,看起来他们必须一次一个分配一个 –

回答

1

取决于那700行其他代码的作用......默认情况下,S4类的initialize方法是一个拷贝构造函数,将未命名的参数作为基类的实例。也许你的意思是你有这样的事情(我的类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 
+0

明白了,这是正确的我有一个然后b的实例,并想创建c。谢谢-Glenn –

+0

尝试初始化,仍然关闭一点。我有A,B和C级。超类包含a,b和c。 a是存储值,b是使用输入的函数调用的结果,c是使用来自a和b的输入的函数调用的结果。我似乎无法创建超类。 –

+0

在这种情况下''setClassUnion'可能有用吗? – ctbrown