我正在开发一种S4方法,该方法需要使用S3对象。 S4方法可与S4对象var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
配合使用。我想将其用于具有类
obj
b
class(b)
[1] "list" "ts"
的类可以是任何东西,只要它们是s3。以下是代码(改编自Haldley Wickham's Advanced R):
b
我得到以下输出和错误:
setOldClass("ts")
setClass("A")
setClass("A1", contains = "A")
setClass("A2", contains = "A1")
setClass("A3", contains = "A2")
setGeneric("bar",function(a) standardGeneric("bar"))
setMethod("bar",signature(a = "A"),function(a) "A here")
setMethod("bar",signature(a = "ts"),function(a) "ts here")
#setMethod("bar",signature(a = "list"),function(a) "list here")
obj <- new("A1")
bar(obj)
b = list(no = 1:3,let=c("a","b"))
class(b) = c("list","ts")
bar(b)
跟踪:
[1] "bar"
[1] "A here"
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘bar’ for signature ‘"list"’
我知道我可以使用签名为3.stop(gettextf("unable to find an inherited method for function %s for signature %s", sQuote(fdef@generic), sQuote(cnames)), domain = NA)
2.(function (classes, fdef, mtable) { methods <- .findInheritedMethods(classes, fdef, mtable) if (length(methods) == 1L) ...
1.bar(b)
的新方法,并且它可以工作。但是我不想那样做。我想使用list
作为签名。我该怎么办?
在我看来,如果我在S3对象中具有多重继承,那么如果S4泛型使用它,它只会查找ts
向量返回的第一类。如何定义也可以查看其他类的方法?