setClass("shape", representation( area = "numeric") ) setClass("triangle", representation("shape", a = "numeric", b="numeric", c="numeric") ) tr1 <- new("triangle", a=3,b=4,c=5) setClass("rectangle", representation("shape", a = "numeric", b="numeric") ) rtgl1 <- new("rectangle", a=3,b=4) setClass("square", representation("shape", a = "numeric") ) sq1 <- new("square", a=3) setClass("lgtdl", representation(times="numeric", values="numeric")) lgtdl1 <- new("lgtdl", times=1:10, values=rnorm(10)) lgtdl2 <- new("lgtdl", times=1:10, values=runif(10)) setMethod("Arith", signature(e1="lgtdl", e2="lgtdl"), function(e1, e2) { if( !identical(e1@times, e2@times) ) stop("can only add lgtdl's with identical times") e1@values = callGeneric(e1@values, e2@values) e1 }) ##old triangle code tr1 <- new("triangle", a=3, b=4, c=5) ##Law of cosines: a^2 = b^2+c^2-2*b*c*cos(A) threesidestoangle <- function(a=a, b=b, c=c) acos((b^2+c^2-a^2)/(2*b*c)) angleA <- function(object) threesidestoangle(a=object@a,b=object@b, c=object@c) angleB <- function(object) threesidestoangle(object@b, object@c, object@a) angleC <- function(object) threesidestoangle(object@c, object@a, object@b) if( ! isGeneric("area") ) setGeneric("area", function(object) standardGeneric("area")) setMethod("area", signature(object="triangle"), function(object) object@a*object@b*sin(angleC(object))/2 ) area(tr1)