title: “OOprgramming” author: “Xinyu Jiao” date: “2016年5月9日” output: html_document —
R has embedded many object inside,vector is the generic type,including character,numeric,logic…
#all initialized to 0
x<-vector(mode = "numeric",length = 10)
#all initialized to FALSE
x<-vector(mode = "logical",length = 5)
#all initialized to ""
x<-vector(mode = "character",length = 3)
#use c() for generic vector
x <- c('hello', 'goodbye')
class(x)
## [1] "character"
x <- 1
class(x)
## [1] "numeric"
x<-c(T,F,T)
class(x)
## [1] "logical"
x <- data.frame(a = 1:10, b = 2:11)
class(x)
## [1] "data.frame"
testFunc <- function(x) x + 1
testFunc(2)
## [1] 3
class(testFunc)#function is also an object
## [1] "function"
x <- table(c('M', 'M', 'F', 'F'),c('White', 'Af-Am', 'Af-Am', 'Af-Am'))
class(x)
## [1] "table"
-Building blocks of a programming language -Organize our work by creating specific “types” of things -data.frames are collections of vectors of different types -glm objects store a bunch of attributes related to linear models -Other people can write functions or scripts operating on those objects, knowing that there will be a consistent structure
-generic functions,operate on different objects but has the same name
summary('hello')
## Length Class Mode
## 1 character character
summary(c(1, 1, 0, 0, 0))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0 0.0 0.0 0.4 1.0 1.0
data members and their type:name,hp,attack,defense,type are all attributes and they take in difference data type, character,numeric,etc
setClass("avengers",
slots = c(
name = 'character',
hp = 'numeric',
mp='numeric',
attack = 'numeric',
defense = 'numeric',
team = 'character'
))
getSlots('avengers')
## name hp mp attack defense team
## "character" "numeric" "numeric" "numeric" "numeric" "character"
ironman <- new("avengers",
name = "Tony Stark",# Note how we're passing in parameters
hp = 160,
mp=0,
attack = 110,
defense = 65,
team = 'ironman')
warmachine <- new("avengers",
name = "Roudy",# Note how we're passing in parameters
hp = 130,
mp=0,
attack = 90,
defense = 60,
team = 'ironman')
vision <- new("avengers",
name = "Vision",# Note how we're passing in parameters
hp = 200,
mp=100,
attack = 130,
defense = 90,
team = 'ironman')
spiderman <- new("avengers",
name = "Peter Parker",# Note how we're passing in parameters
hp = 120,
mp=0,
attack = 100,
defense = 50,
team = 'ironman')
panther <- new("avengers",
name = "Tchala",# Note how we're passing in parameters
hp = 130,
mp=0,
attack = 120,
defense = 70,
team = 'ironman')
widow <- new("avengers",
name = "Natasha Romannoff",# Note how we're passing in parameters
hp = 90,
mp=0,
attack = 90,
defense = 50,
team = 'ironman')
captain <- new("avengers",
name = "Steve Rogers",# Note how we're passing in parameters
hp = 150,
mp=0,
attack = 110,
defense = 65,
team = 'captain')
witch <- new("avengers",
name = "Wanda",# Note how we're passing in parameters
hp = 130,
mp=200,
attack = 110,
defense = 70,
team = 'captain')
falcon <- new("avengers",
name = "Sam",# Note how we're passing in parameters
hp = 100,
mp=0,
attack = 100,
defense = 60,
team = 'captain')
antman <- new("avengers",
name = "Scott Lang",# Note how we're passing in parameters
hp = 120,
mp=0,
attack = 100,
defense = 50,
team = 'captain')
wintersoilder <- new("avengers",
name = "Buchy Barnes",# Note how we're passing in parameters
hp = 130,
mp=0,
attack = 100,
defense = 60,
team = 'captain')
hawkeye <- new("avengers",
name = "Clint Barton",# Note how we're passing in parameters
hp = 90,
mp=0,
attack = 90,
defense = 50,
team = 'captain')
setMethod("print", #name of the internal function
signature = "avengers", # Means that the print function operates on the "pokemon" class
function(x){
cat(sprintf('%s, go! This %s ally has an attack strength of %s.',
toupper(x@name),
x@team,
x@attack))
})
## Creating a generic function for 'print' from package 'base' in the global environment
## [1] "print"
print(vision)
## VISION, go! This ironman ally has an attack strength of 130.
setClass("shield", #child class name
contains = "avengers", # What it inherits from
slots = c(
sleep = 'numeric' # New attributse for a snorlax
),
prototype = list(name = 'shield', # defaults parameters
hp = 160,
attack = 110,
defense = 65,
team = 'neutral')
)
coulson <- new("shield", sleep = 12, attack = 0)
print(coulson)
## SHIELD, go! This neutral ally has an attack strength of 0.
setGeneric("fight", function(object){
standardGeneric("fight")
})
## [1] "fight"
fight.civil <- function(object1,object2){
r1 <- rnorm(1, mean = object1@attack, sd = object1@defense/5)
r2 <- rnorm(1, mean = object2@attack, sd = object2@defense/5)
if(r1 > r2){
cat(sprintf('%s has won over %s, caused damage %s!',
toupper(object1@name),
object2@name,
object1@attack-object2@defense))
} else if(r1 < r2) {
cat(sprintf('%s has won over %s, caused damage %s!',
toupper(object2@name),
object1@name,
object2@attack-object1@defense))
} else {
cat("It's even.\n")
}
return("end")
}
#setMethod("fight", "avengers", fight.civil)
#generic function fight, when fight operate on pokemon, it functions as fight.pokemon
fight.civil(ironman,captain)
## STEVE ROGERS has won over Tony Stark, caused damage 45!
## [1] "end"
setClass(Class = "node",
slots = c(
price="numeric",
strike="numeric",
option="character",
maturity="numeric"
)
)
myopt<-new(Class = "node",
price=100,
strike=100,
option="american",
maturity=1)
setClass(Class = "node",
slots = c(
price="numeric",
strike="numeric",
option="character",
maturity="numeric",
left="node",
right="node"
)
)
node.left<-new("node",
price=110,
strike=100,
option="american",
maturity=1)
node.right<-new("node",
price=90,
strike=100,
option="american",
maturity=1)
am.opt<-new("node",
price=100,
strike=100,
option="american",
maturity=1,
left=node.left,
right=node.right)
am.opt@left@price
## [1] 110
am.opt@price
## [1] 100
am.opt@right@price
## [1] 90