OBJECTORIENTED PROGRAMMING IN [R]

上传人:桔**** 文档编号:570570219 上传时间:2024-08-05 格式:PPT 页数:21 大小:537.50KB
返回 下载 相关 举报
OBJECTORIENTED PROGRAMMING IN [R]_第1页
第1页 / 共21页
OBJECTORIENTED PROGRAMMING IN [R]_第2页
第2页 / 共21页
OBJECTORIENTED PROGRAMMING IN [R]_第3页
第3页 / 共21页
OBJECTORIENTED PROGRAMMING IN [R]_第4页
第4页 / 共21页
OBJECTORIENTED PROGRAMMING IN [R]_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《OBJECTORIENTED PROGRAMMING IN [R]》由会员分享,可在线阅读,更多相关《OBJECTORIENTED PROGRAMMING IN [R](21页珍藏版)》请在金锄头文库上搜索。

1、Henrik Bengtssonhbmaths.lth.se(MSc Computer Science, PhD candidate in Statistics)Mathematical StatisticsCentre for Mathematical SciencesLund University, SwedenObject-oriented programming and programming style guidelines for R1OutlineObjects and ClassesConcepts of object-oriented programmingA complet

2、e example in R ShapesReferences in RR Programming Style Guidelines with a few coding conventions.2Part I:Object-oriented programming in R3Objects and ClassesMicroarrayDatalayout: LayoutR: doubleG: doubleRb: doubleGb: doublenbrOfSlides(): intnbrOfSpots(): intswapDyes(.)append()as.data.frame(): data.f

3、ramegetLayout(): LayoutsetLayout(layout)subtractBackground(.)normalizeWithinSlide(.) normalizeAcrossSlides(.)plot(.)plotSpatial(.)boxplot(.)hist(.)static read(.): MicroarrayDatawrite(.)Class nameFieldsMethodsMicroarrayDataLayoutMicroarrayDataMicroarrayDataa class is a data type -an object is an inst

4、ance of a classObjects of different classesA class is the recipe for a certain cake.and the objects are the actual cakes of that kind.4Encapsulation, Inheritance, and PolymorphismEncapsulation means that a group of related properties, methods, and other members are treated as a single unit or object

5、. Objects can control how properties are changed and methods are executed. Why: Makes it easier to change your implementation at a later date by letting you hide implementation details of your objects, a practice called data hiding. 5Encapsulation, Inheritance, and PolymorphismInheritance describes

6、the ability to create new classes based on an existing class. The new class inherits all the properties and methods and events of the base class, and can be customized with additional properties and methods. Why: Promotes code reuse since the code for the methods of the subclasses do not need to be

7、rewritten.6Encapsulation, Inheritance, and PolymorphismPolymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. Polymorphism is essential to object-oriented programming because it allow

8、s you to use items with the same names, no matter what type of object is in use at the moment.Why: Inheritance becomes more flexible. Subclasses can keep some methods inherited from their super classes and override others.7Overloading and OverridingOverloaded members are used to provide different ve

9、rsions of a property or method that have the same name, but that accept a different number of parameters, or parameters with different data types. Currently not supported in R.Overridden properties and methods are used to replace an inherited property or method that is not appropriate in a derived c

10、lass. Overridden members must accept the same data type and number of arguments (not enforced in R). Derived classes inherit overridden members.8Unified Modeling Language (UML) class diagram10.*abstractstaticprivateassociation(“using”)inheritance(“is a”)9# Create different Shape objects and store th

11、em in a listallShapes - list( Rectangle(Point(0,0), width=5, height=8, color=blue), Square(Point(-2,-5), side=3, color=red), Triangle(Point(3,3), width=10, height=12, color=orange), Triangle(Point(-4,-2.5), width=12, height=3, color=purple), Circle(Point(-4,4), radius=5, color=green)# Plot all shape

12、sfor shape in allShapes paint(shape)# Get first mouse clickclick - getFromClick(Point)while click is inside plot region # Check with all shapes if they contains the click coordinates. for shape in allShapes if contains(shape, click) then paint(click, col=getColor(shape), style=disc) else paint(click

13、, style=circle) # Get another mouse click click - getFromClick(Point)Interactive examplepolymorphismstatic method callEither shape$contains(click) orcontains(shape, click)10setClassS3(Point, function(x=0, y=0) extend(Object(), Point, .x = x, # private .y = y # private ); )setMethodS3(getX, Point, fu

14、nction(this) this$.x; )setMethodS3(getY, Point, function(this) this$.y; )setMethodS3(getXY, Point, function(this) c(this$.x, this$.y); )setMethodS3(setX, Point, function(this, newX) this$.x - newX; # Using reference!)setMethodS3(setY, Point, function(this, newY) this$.y - newY;)setMethodS3(setXY, Po

15、int, function(this, newXY) this$.x - newXY1; this$.y - newXY2;)setMethodS3(getFromClick, Point, function(this) xy - locator(n=1); # Ask for one mouse click Point(x=xy$x, y=xy$y);)setMethodS3(print, Point, function(this) print(sprintf(%s at (%.3f,%.3f)., getClass(this), this$.x, this$.y);)setMethodS3

16、(paint, Point, function(this, .) points(this$.x, this$.y, .);)privatestaticclassmethodCode for a class11ReferencesOne reference can only refer to one object. One object can have one or several references referring to it.c - Circle(Point(0,0), radius=2);c1 - c;setRadius(c1, 4);getRadius(c); # will gi

17、ve 4!Results in more user-friendly packages for the end user!Makes design and implementation much easier.Saves memory.12References, how?References are not supported by R since everything is copy-by-value. Have to return new instance:setValue - function(list, value) list$value; return(list);What you

18、really want to do:setValue - function(list, value) list$value;Why: For example, each of the Shape objects can use (refer to) the same Point object to specify its position. By moving the Point object, all Shape object will then move along. This is not possible without references.However, reference ca

19、n be emulated by encapsulating such functionalities in a root class Object, which all classes are enforced to be derived from. Contact me to get the code for Object, setClassS3() and setMethodS3().13Part II:R Programming Style Guidelines14Programming Style Guidelines80% of the lifetime cost of a pie

20、ce of software goes to maintenance. Hardly any software is maintained for its whole life by the original author. Code conventions improve the readability of the software, allowing programmers to understand new code more quickly and thoroughly.If you ship your source code as a product, you need to ma

21、ke sure it is as well packaged and clean as any other product you create.15R Coding ConventionCurrently there is no RCC and people invent their own conventions or not at all.We suggest to adapt a modified version of the Java coding convention, which has proved to be successful and is a de facto stan

22、dard.16Class namesNames representing classes must be nouns and written in mixed case starting with upper case.Shape, Rectangle, Point, MicroarrayData, LayoutAvoid . (period) in class names, because it might lead to ambiguities. , e.g. my.very.own.class is not a good name.17Field and variable namesVa

23、riables and fields names must be in mixed case starting with lower case. x, y, nbrOfSlides, locusTo maintain readability of the code, do not shorten variable names, e.g. nbrOfGrids (or ngrids) is much better than ngr.Avoid using . (period) in variable names to make names more consistent with other n

24、aming conventions. However, private fields, e.g. layout., may contain periods for improving readability.18Method namesNames representing methods (functions) must be verbs and written in mixed case starting with lower case.getLayout(), normalize(method, slides)To maintain readability of the code, do

25、not shorten method names, e.g. normalizeWithinSlides() is much better than normWSl().For same reasons as before avoid using . (period) in method names, e.g. get.layout() is not good.19File namesClasses should be declared in individual files with the file name matching the class name.Point.R, GenePix

26、Data.RResults in well organized file structure and also gives quick access to the source code, Listing all *.R files in a source directory will give you an overview of all the classes.For stand-alone functions one may adapt the same policy;intToHex.R, col2rgb.R20Where to startTutorials and source code:R Programming Style GuidelinesProgramming with References in RImplementing support for references in Rhttp:/www.maths.lth.se/help/R/21

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号