Menu
CoddyTech

callNextMethod

Termasuk bagian Pemrograman Berorientasi Object dalam perjalanan R di Coddy. Pelajaran 33 dari 57.

Di dalam method S4, callNextMethod() memanggil method dari class induk dengan argumen yang sama dan mengembalikan hasilnya. Method anak mengembangkan jawaban dari induknya, alih-alih mengulanginya:

setClass("Employee", slots = c(name = "character", salary = "numeric"))
setClass("Manager", contains = "Employee", slots = c(reports = "numeric"))
invisible(setGeneric("describe", function(x) standardGeneric("describe")))
setMethod("describe", "Employee", function(x) paste(x@name, "earns", x@salary))
setMethod("describe", "Manager", function(x) paste(callNextMethod(), "and manages", x@reports))
print(describe(new("Manager", name = "Ada", salary = 60000, reports = 4)))

Output:

[1] "Ada earns 60000 and manages 4"

Cara ini juga berfungsi untuk show(): child mencetak baris milik parent, lalu barisnya sendiri:

setClass("Employee", slots = c(name = "character"))
setClass("Manager", contains = "Employee", slots = c(reports = "numeric"))
setMethod("show", "Employee", function(object) cat("Employee:", object@name, "\n"))
setMethod("show", "Manager", function(object) {
  callNextMethod()
  cat("Reports:", object@reports, "\n")
})
new("Manager", name = "Ada", reports = 4)

Output:

Employee: Ada 
Reports: 4 

new() memanggil initialize() generik. Method untuk itu dapat mengisi slot yang dihitung dari slot lainnya. Method tersebut terlebih dahulu memanggil callNextMethod(.Object, ...), sehingga penyiapan normal dan pemeriksaan validitas tetap dilakukan:

setClass("Order", slots = c(prices = "numeric", total = "numeric"))
setMethod("initialize", "Order", function(.Object, ...) {
  .Object <- callNextMethod(.Object, ...)
  .Object@total <- sum(.Object@prices)
  .Object
})
o <- new("Order", prices = c(2.5, 4, 1))
print(o@total)

Output:

[1] 7.5

Seperti NextMethod() di S3, callNextMethod() merangkai beberapa tingkat, dan setiap class menambahkan bagiannya:

setClass("Ticket", slots = c(base = "numeric"))
setClass("Vip", contains = "Ticket")
setClass("Backstage", contains = "Vip")
invisible(setGeneric("price", function(t) standardGeneric("price")))
setMethod("price", "Ticket", function(t) t@base)
setMethod("price", "Vip", function(t) callNextMethod() * 2)
setMethod("price", "Backstage", function(t) callNextMethod() + 50)
cat(price(new("Ticket", base = 30)), price(new("Vip", base = 30)), price(new("Backstage", base = 30)))

Output:

30 60 110
challenge icon

Tantangan

Mudah

Tiket tersedia dalam tiga tingkat, masing-masing dalam filenya sendiri. Class dan price() generik sudah dideklarasikan. Tambahkan method berikut:

  • Ticket.R: price() adalah harga dasar; show() mencetak Ticket for gala dan baris baru.
  • Vip.R: price() adalah 1.5 kali harga tiket; show() mencetak baris tiket, lalu VIP lounge access.
  • Backstage.R: price() adalah harga VIP ditambah 50; show() mencetak baris VIP, lalu Backstage pass.

Gunakan callNextMethod() dalam method subclass. Kode yang disediakan membaca baris vip gala 80 (tingkat, acara, harga dasar), lalu mencetak setiap tiket dan harganya.

Tuliskan kodemu di Ticket.R, Vip.R, dan Backstage.R. main.R berisi kode input/output yang disediakan dan tidak boleh diedit.

Coba sendiri

source("Ticket.R")
source("Vip.R")
source("Backstage.R")

# Kode input/output yang disediakan: biarkan apa adanya
input <- suppressWarnings(readLines(file("stdin")))
for (line in input) {
  p <- strsplit(line, " ")[[1]]
  base <- as.numeric(p[3])
  t <- switch(p[1],
    standard = new("Ticket", event = p[2], base = base),
    vip = new("VipTicket", event = p[2], base = base),
    backstage = new("BackstageTicket", event = p[2], base = base))
  print(t)
  cat(sprintf("price: %.2f", price(t)), "\n", sep = "")
}
quiz iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Pemrograman Berorientasi Object

Latihan sendiri: Compiler R online