เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Exposing your Parent

By default, R6 classes only have access to the functionality of their direct parent. To allow access across multiple generations, the intermediate classes need to define an active binding that exposes their parent. This takes the form

active = list(
  super_ = function() super
)

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Object-Oriented Programming with S3 and R6 in R

ดูคอร์ส

คำแนะนำการฝึกหัด

A microwave oven class has been predefined for you in your workspace.

  • Extend the fancy microwave oven class by adding an active list element.
  • Add an active binding to expose its parent's functionality.
    • The binding should be called super_.
    • It should be a function that takes no arguments and simply returns super.
  • Instantiate a fancy microwave object and assign it to a_fancy_microwave.
  • Call the fancy microwave's super_ binding.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Expose the parent functionality
fancy_microwave_oven_factory <- R6Class(
  "FancyMicrowaveOven",
  inherit = microwave_oven_factory,
  public = list(
    cook_baked_potato = function() {
      self$cook(3)
    },
    cook = function(time_seconds) {
      super$cook(time_seconds)
      message("Enjoy your dinner!")
    }
  ),
  # Add an active element with a super_ binding
  ___ = ___(
    ___ = ___
  )
)

# Instantiate a fancy microwave
a_fancy_microwave <- ___

# Call the super_ binding
___
แก้ไขและรันโค้ด