- inherience and ADT
setClass("Passenger", representation(name = "character", origin = "character",destination = "character"))#定义类Passenger
setClass("FreqFlyer", representation(ffnumber = "numeric"), contains = "Passenger")#定义类FreqFlyer,且继续Passenger类
getClass("FreqFlyer")#取类FreqFlyer
subClassNames("Passenger")#取Passenger类的子类名称
superClassNames("FreqFlyer")#取FreqFlyer类的父类名称
setClass("Rectangle", representation(h = "numeric", w = "numeric", area = "numeric"))#定义类Rectangle
myr = new("Rectangle", h = 10, w = 20, area = 200)#实便化这个类,对象的名称为myr
myr
setGeneric("area", function(shape) standardGeneric("area")) #根据ADT定义一个抽象函数
setMethod("area", signature(shape = "Rectangle"), function(shape) shape@area)#定义一个类的方法
myr@area#调用类的方area方法
area(myr)调用area函数,参数为myr
2. S4 OOP
setClass("A", representation(s1 = "numeric"), prototype = prototype(s1 = 0))
myA = new("A")
myA
m2 = new("A", s1 = 10)
m2
setClass("B", contains = "A", representation(s2 = "character"), prototype =list(s2 = "hi"))
myB = new("B")
myB