-1

I am a newbie in R language, so I think i should illustrate this requirement just as Java now we know R can support the OOP, so I want to invoke ClassB method from ClassA

ClassB.java

public class ClassB {
    public void printB() {
       System.out.println("a");
    }
}

ClassA.java

//if they are in the same package, use it directly
//or we must import ClassB explicitly
public class ClassA {
    public void invokeB() {
       ClassB b = new ClassB();
       b.printB();
    }
}

so how can i achieve this in R language?

clevertension
  • 6,929
  • 3
  • 28
  • 33
  • There are several OOP frameworks in R. You'll need to specify which one you're using. And it probably wouldn't hurt to provide some R code the demonstrate what you're trying to do. A lot of R people who would be able to help you probably don't know the first thing about Java. – joran Jun 11 '13 at 14:45
  • thanks joran, can you give some links or names about OOP frameworks in github or other open source repository? – clevertension Jun 11 '13 at 15:05
  • If you aren't even familiar with the major OOP frameworks available in R, then it is too early for you to be asking this question. Do some research first. – joran Jun 11 '13 at 15:09
  • The highly up-voted responses to [this](http://stackoverflow.com/questions/9521651/r-and-object-oriented-programming?lq=1) question provide a quick overview of class systems (with additional links) in R. – Martin Morgan Jun 11 '13 at 20:33

1 Answers1

0

This is how assignment of classes is done in the S3 system of method dispatch. It's not really OOP as Java users understand it. The proto package provides a framework that more closely resembles Java style OOP. The S4 system of method dispatch provided multi-argument signature matching and a more discipline approach... that most R-users cannot understand.

 newItem <- "bbb"
class(newItem) <- "newClass"
print(newItem)
#[1] "bbb"
#attr(,"class")
#[1] "newClass"
 print.newClass <- function(n) cat("this is of the newClass", n)
 print(newItem)
#this is of the newClass bbb
 otherItem <- "xxxx"
 inherits(otherItem, "newClass")
#[1] FALSE

 class(otherItem) <- c( class(otherItem), "newClass")
 print(otherItem)
#this is of the newClass xxxx

When you talk about "classes" being in a "different file", then you probably need to study package construction and the care and feeding of NAMESPACEs. You are able to attach packages as well as load via namespaces. R functions are perhaps somewhat similar to Java classes (in a rather loose use of the English language, but not either Java or R) but in R, the term 'class' refers to an attribute of an object which is used to dispatch functions.

IRTFM
  • 258,963
  • 21
  • 364
  • 487