Menu
CoddyTech

Class Virtual

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

virtual class menjelaskan hal yang dimiliki bersama oleh suatu keluarga class, dan tidak dapat memiliki object sendiri. Deklarasikan dengan representation("VIRTUAL"); subclass-nya menggunakan contains seperti biasa:

setClass("Shape", representation("VIRTUAL"))
setClass("Circle", contains = "Shape", slots = c(r = "numeric"))
setClass("Square", contains = "Shape", slots = c(side = "numeric"))
cat(isVirtualClass("Shape"), is(new("Circle", r = 1), "Shape"), "\n")
cat(tryCatch({ new("Shape"); "made" }, error = function(e) "not allowed"), "\n")

Output:

TRUE TRUE 
not allowed 

Method untuk virtual class berfungsi untuk setiap subclass. Di sini, describe() untuk Shape memanggil area(), yang diimplementasikan oleh setiap subclass, sehingga deskripsi cukup ditulis sekali:

setClass("Shape", representation("VIRTUAL"))
setClass("Circle", contains = "Shape", slots = c(r = "numeric"))
setClass("Square", contains = "Shape", slots = c(side = "numeric"))
invisible(setGeneric("area", function(s) standardGeneric("area")))
invisible(setGeneric("describe", function(s) standardGeneric("describe")))
setMethod("area", "Circle", function(s) pi * s@r^2)
setMethod("area", "Square", function(s) s@side^2)
setMethod("describe", "Shape", function(s) sprintf("%s with area %.2f", class(s), area(s)))
for (s in list(new("Circle", r = 1), new("Square", side = 2))) cat(describe(s), "\n")

Output:

Circle with area 3.14 
Square with area 4.00 

class virtual dapat mendeklarasikan slot, yang kemudian dimiliki setiap subclass:

setClass("Shape", representation("VIRTUAL", label = "character"))
setClass("Square", contains = "Shape", slots = c(side = "numeric"))
sq <- new("Square", label = "tile", side = 2)
print(slotNames("Square"))

Output:

[1] "side"  "label"

setClassUnion() membuat class virtual dari class yang sudah ada. Slot dengan tipe tersebut menerima salah satu class itu, seperti kontak yang berupa alamat email atau nomor telepon:

setClassUnion("Contact", c("character", "numeric"))
setClass("Friend", slots = c(name = "character", contact = "Contact"))
a <- new("Friend", name = "Ada", contact = "ada@x.org")
b <- new("Friend", name = "Bo", contact = 5550123)
cat(class(a@contact), class(b@contact), "\n")
cat(tryCatch({ new("Friend", name = "Cy", contact = TRUE); "ok" }, error = function(e) "refused"), "\n")

Output:

character numeric 
refused 
challenge icon

Tantangan

Mudah

Pembayaran menggunakan class virtual Payment (slot amount) dan generic fee() serta receipt(), semuanya ada di Payment.R. Lengkapi:

  • di Payment.R, method receipt() untuk Payment: card: 100.00 + 2.00 fee, yang dibuat dari nama class dalam huruf kecil, amount, dan fee();
  • di Card.R, class Card (yang berisi Payment) dan fee()-nya: 2% dari jumlah;
  • di Cash.R, class Cash dan fee()-nya: 0.

Kode yang disediakan membaca baris card 100 atau cash 20, mencetak setiap tanda terima dan total tagihan, lalu memeriksa bahwa Payment bersifat virtual dan tidak dapat dibuat.

Tulis kodemu di Payment.R, Card.R, dan Cash.R. main.R berisi kode input/output yang disediakan dan tidak dapat diedit.

Coba sendiri

source("Payment.R")
source("Card.R")
source("Cash.R")

# Kode input/output yang disediakan: biarkan apa adanya
input <- suppressWarnings(readLines(file("stdin")))
total <- 0
for (line in input) {
  p <- strsplit(line, " ")[[1]]
  amount <- as.numeric(p[2])
  pay <- switch(p[1], card = new("Card", amount = amount), cash = new("Cash", amount = amount))
  cat(receipt(pay), "\n", sep = "")
  total <- total + amount + fee(pay)
}
cat(sprintf("charged: %.2f", total), "\n", sep = "")
cat("Payment is virtual: ", isVirtualClass("Payment"), "\n", sep = "")
cat(tryCatch({ new("Payment", amount = 1); "created a Payment" }, error = function(e) "cannot create a Payment"), "\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