Object-Oriented Programming

本文介绍了面向对象编程的基本概念,包括抽象数据类型(ADT),如何通过继承机制创建和使用复杂的数据类型,以及如何将现实世界的行为模拟为计算模型。以俄罗斯方块游戏为例,详细解释了如何使用OOP进行游戏设计。

Object-oriented programming (OOP) is programming primarily focused on data, while data and behavior are being inseparably linked. Data and behavior together constitute a class, while object are class instances.

OOP consider computation as modeling of behavior. The modeled item is the object represented by computational abstractions. Suppose we want to write a well known game "Tetris". To do this, we must learn how to model the appearance of random shapes composed of four squares joined together by edges. Also we need to regulate the falling speed of shapes, define operations of rotation and shift of shapes. Moving of shapes is limited by the well's boundaries, this requirement must also be modeled. Besides that, filled rows of cubes must be destroyed and achieved points must be counted.

Thus, this easy to understand game requires the creation of several models - shape model, well model, shape movement model and so on. All these models are abstractions, represented by calculation in the computer. To describe these models, the concept of Abstract Data Type, ADT(or complex data type) is used. Strictly speaking, the model of "shapes" motion in DOM is not a data type, but it is a set of operations on the "shape" data type, using the restrictions of "well" data type.

Objects are class variables. Object-oriented programming allows you to easily create and use ADT. Object oriented programming uses the inheritance mechanism. The benefit of inheritance is the face that it allows obtaining derivative types from data types already defined by a user.

For example, to create Tetris shapes, it's convenient to create a base class Shape first. The other classes representing all seven possible shape types can be derived on its basis. Behavior of shapes is defined in the base class, while implementation of behavior of each separator shape is defined in derivative classes.

In OOP objects are responsible for their behavior. ADT developer should include a code to describe any behavior that would normally be expected from the corresponding objects. The fact that the object is responsible for its behavior, greatly simplifies the task of programming for the user of this object. If we want to draw a shape on the screen, we need to know where the center will be and how to draw it. if a separator shape know how to draw itself, the programmer should send a "draw" message when using such a shape.

The concept of OOP has a set of related concepts, including the following:

Simulated of actions from the real world

user-defined data types

Hiding the implementation details

Possibility of code reuse through inheritance

interpretation of function calls during execution

some of these concept are rather vague, some are abstract, others are general.

Title: R Object-Oriented Programming Author: Kelly Black Length: 190 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-10-23 ISBN-10: 1783986689 ISBN-13: 9781783986682 A practical guide to help you learn and understand the programming techniques necessary to exploit the full power of R About This Book Learn and understand the programming techniques necessary to solve specific problems and speed up development processes for statistical models and applications Explore the fundamentals of building objects and how they program individual aspects of larger data designs Step-by-step guide to understand how OOP can be applied to application and data models within R Who This Book Is For This book is designed for people with some experience in basic programming practices. It is also assumed that they have some basic experience using R and are familiar using the command line in an R environment. Our primary goal is to raise a beginner to a more advanced level to make him/her more comfortable creating programs and extending R to solve common problems. In Detail R is best suited to produce data and visual analytics through customizable scripts and commands, instead of typical statistical tools that provide tick boxes and drop-down menus for users. The book is divided into three parts to help you perform these steps. It starts by providing you with an overview of the basic data types, data structures, and tools available in R that are used to solve common tasks. It then moves on to offer insights and examples on object-oriented programming with R; this includes an introduction to the basic control structures available in R with examples. It also includes details on how to implement S3 and S4 classes. Finally, the book provides three detailed examples that demonstrate how to bring all of these ideas together. Table of Contents Chapter 1. Data Types Chapter 2. Organizing Data Chapter 3. Saving Data and Printing Results Chapter 4. Calculating Probabilities and Random Numbers Chapter 5. Character and String Operations Chapter 6. Converting and Defining Time Variables Chapter 7. Basic Programming Chapter 8. S3 Classes Chapter 9. S4 Classes Chapter 10. Case Study – Course Grades Chapter 11. Case Study – Simulation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值