$names
[1] "name" "salary" "union"
$class
[1] "employee"
Before we write a print method for this class, let’s see what happens
when we call the defaultprint():
j
$name
[1] "Joe"
$salary
[1] 55000
$union
[1] TRUE
attr(,"class")
[1] "employee"
Essentially,jwas treated as a list for printing purposes.
Now let’s write our own print method:
print.employee <- function(wrkr) {
cat(wrkr$name,"\n")
cat("salary",wrkr$salary,"\n")
cat("union member",wrkr$union,"\n")
}
So, any call toprint()on an object of class"employee"should now be
referred toprint.employee(). We can check that formally:
methods(,"employee")
[1] print.employee
Or, of course, we can simply try it out:
j
Joe
salary 55000
union member TRUE
Object-Oriented Programming 213