Menu
Coddy logo textTech

İki Argümana Göre Gönderim

Coddy'nin R Journey'sinin Nesne Tabanlı Programlama bölümünün bir parçası. Ders 29 / 57.

Bir S4 generic'i, metodunu birden çok argümanın sınıflarına göre seçebilir. İmza, her argüman için bir sınıf listeler; bu nedenle her sınıf çifti kendi metoduna sahip olabilir:

setClass("Asteroid", slots = c(size = "numeric"))
setClass("Ship", slots = c(name = "character"))
invisible(setGeneric("collide", function(a, b) standardGeneric("collide")))
setMethod("collide", signature("Asteroid", "Ship"), function(a, b) paste(b@name, "is hit"))
setMethod("collide", signature("Ship", "Ship"), function(a, b) "two ships bump")
rock <- new("Asteroid", size = 3)
enterprise <- new("Ship", name = "Enterprise")
print(collide(rock, enterprise))
print(collide(enterprise, enterprise))

Çıktı:

[1] "Enterprise is hit"
[1] "two ships bump"

"ANY" sınıfı her sınıfla eşleşir. signature("ANY", "ANY") için bir yöntem, daha özel hiçbir çift uymadığında çalışan geri dönüş yöntemidir:

setClass("Ship", slots = c(name = "character"))
invisible(setGeneric("collide", function(a, b) standardGeneric("collide")))
setMethod("collide", signature("Ship", "Ship"), function(a, b) "two ships bump")
setMethod("collide", signature("ANY", "ANY"), function(a, b) "nothing happens")
s <- new("Ship", name = "A")
cat(collide(s, s), "/", collide(s, 42), "/", collide("x", "y"))

Çıktı:

two ships bump / nothing happens / nothing happens

Bağımsız değişkenlerin sırası önemlidir: signature("A", "B") ve signature("B", "A") farklı yöntemlerdir. numeric ve character gibi temel sınıflar da bir imzada görünebilir:

invisible(setGeneric("join", function(x, y) standardGeneric("join")))
setMethod("join", signature("character", "numeric"), function(x, y) paste(x, "number", y))
setMethod("join", signature("numeric", "character"), function(x, y) paste(y, "after number", x))
print(join("item", 3))
print(join(3, "item"))

Çıktı:

[1] "item number 3"
[1] "item after number 3"

Bir imzadaki "missing" sınıfı, hiç verilmemiş bir bağımsız değişkenle eşleşir; böylece tek bir generic kısa ve uzun biçim sunabilir:

invisible(setGeneric("greet", function(name, greeting) standardGeneric("greet")))
setMethod("greet", signature("character", "missing"), function(name, greeting) paste("Hello,", name))
setMethod("greet", signature("character", "character"), function(name, greeting) paste0(greeting, ", ", name))
print(greet("Ada"))
print(greet("Ada", "Good morning"))

Çıktı:

[1] "Hello, Ada"
[1] "Good morning, Ada"
challenge icon

Görev

Kolay

Kargo fiyatları ürüne ve bölgeye bağlıdır. Sınıflar Item.R (Letter, Box; her birinde kg yuvası bulunur) ve Zone.R (Domestic, Abroad; her birinde name yuvası bulunur) içinde tanımlanır. Rates.R içinde genel shipping(item, zone) işlevini ve her çift için bir yöntem oluşturun:

  • Evde Letter: 1; yurt dışında: 3.
  • Evde Box: 5 artı kg başına 2; yurt dışında: 12 artı kg başına 4.
  • Diğer tüm çiftler: no rate for this item and zone ile durdurun (call. = FALSE kullanın).

Verilen kod, bilinmeyen kelimelerin düz metin olarak kaldığı box home 2.5 (ürün, bölge, kg) satırlarını okur ve box to home: 10.00 ya da error: iletisini ve mesajı yazdırır.

Kodunuz Item.R, Zone.R ve Rates.R dosyalarına gider. main.R, verilen giriş/çıkış kodunu içerir ve düzenlenemez.

Kendin dene

source("Item.R")
source("Zone.R")
source("Rates.R")

# Sağlanan girdi/çıktı kodu: olduğu gibi bırakın
input <- suppressWarnings(readLines(file("stdin")))
for (line in input) {
  p <- strsplit(line, " ")[[1]]
  kg <- as.numeric(p[3])
  item <- switch(p[1], letter = new("Letter", kg = kg), box = new("Box", kg = kg), kg)
  zone <- switch(p[2], home = new("Domestic", name = "home"), abroad = new("Abroad", name = "abroad"), p[2])
  out <- tryCatch(sprintf("%s to %s: %.2f", p[1], p[2], shipping(item, zone)),
                  error = function(e) paste("error:", conditionMessage(e)))
  cat(out, "\n", sep = "")
}
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Nesne Tabanlı Programlama bölümündeki tüm dersler

Kendi başına pratik yap: Online R derleyicisi