2013-09-29 74 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