4. The Ball Class PROPERTIES Size Base_Color Elasticity Shape Decal? METHODS Roll Bounce Curve? Fly Spin
5.
6.
7.
Notas del editor
This evening’s class will start with a brief introduction to classes, a concept central to the notion of object-oriented programming. By the time we have finished this class you will have examined concepts related to object-oriented programming and be able to articulate the meaning and utility of classes in programming.
Our agenda is a short one:
We will begin our discussion on classes with an exploration of object-oriented development, an approach to programming that starts by dealing with entities in the code as “objects” that can be manipulated and examined like objects in the real-world. Our code so far has been fairly simple, but at the same time quite verbose – that is, we have had to write a lot of code – often repeatedly – in order to get our solutions to work. We have learned that some techniques like branching, looping and the development of methods can allow us to write more elegant (or tighter) code. OOD allows us to go a step further by modelling real-world objects so that we can use them more effectively. A Class in programming terms is the model of an object: something that represents a thing and can describe properties or attributes of the object as well as the tings it can do – its methods or behaviours. For example, if we wanted to develop a game in which we needed to model a car, we would need to describe the object with the properties as well as the actions that the car can undertake. So how do we describe a car? What are its attributes? [type, model, maker, ext_color, int_color, style, engine_size, hp, warranty, int_fabric]. And what can a car do [drive, backup, accelerate, park] Class creation and manipulation requires certain rules and procedures and those are largely determined by the language. Ruby is a very simple yet robust language that minimizes the rules and procedures needed to create and manipulate its Classes. In addition, Ruby has many built-in classes – some of which we will examine shortly. These built-in classes are part of the default set of capabilities that the language is able to work with and in fact that we have already been working with since the beginning of our work in Ruby. Why use classes? To begin, being able to abstract – or model – an object as a Class allows us to use it as a blueprint or cookie-cutter so we can create numerous instances of the object and manipulate its properties and behaviours. This allows us to re-use the code many times rather than having to write it every time we want to use an object in our code. Second, as a developer if I write a piece of code and want to re-sell that application, I can encapsulate the class into an encrypted object like a java .jar file or a Windows .dll and hide the details of the code that makes it work – protecting my investment and my intellectual property. If I want to use someone else’s class I may be able to simply attach the class to my application and call its properties and methods by following a published API (application programming interface). The API tells us what I can pass to the object and what I will get back from it…I don’t really need to know what’s inside of it. Finally, I can create classes based on other classes – so that if I need to have an object that is like some other object, but has some unique capabilities or features, I can create a Class based on another (its parent) and then just add the additional properties or functions to it. I can also use functions that are in one class across numerous classes through something called polymorphism – as long as I follow some basic rules. Overall, then, Classes allow me to create a model of a business object and deploy or re-use instances of that object that I can manipulate in code. Let’s try an example: let’s think about the idea of a ball. I want you to write down two columns for our class “Ball”. What are all the things that describe a ball (the features that might make one ball different from or similar to another ball) and what are the things a ball can do (something that all balls may be made to do)…
Well here’s the start of a model or abstraction of the Ball Class. The basic attributes might include…(see image) And the basic behaviours could include…(see image) What else did you identify? Are they features of ALL Balls? Now if we instantiate the class Ball by creating a new Ball, then that object should be able to be described by any of those properties and should be able to do any of those methods. If we wanted to create a new class of ball – say Soccer_Ball, then what new properties and methods might we need to add?
Let’s look at some of the built-in classes in Ruby – you can point your browser to the URL shown here and explore some of the Classes in Ruby. We’ve used some of these Classes already, most recently Array – let’s look at that…note the methods listed for the Class “Array” Now look at some others – here are some ideas that might be useful: String, Matrix, Foo (don’t ask), and how about Object! Object is the Mother of ALL Classes in Ruby, so that means that everything that Object can do, every other Class can do too. Looking at parent child relationships, you can see the inheritance in something like DateTime Class – which is a child of Date. We can develop our own classes based on those classes simply by creating a sub-class…or we can create our own class by using the class keyword, capitalizing the class name and defining the properties and methods of the class – remember that to Ruby both properties and behaviours are all methods, but properties are just laid out to allow us to set the value of a property and to get the value of the property (we’ll see this in a moment when I demonstrate a class in Ruby). Finally, to use the Class – you use the “new” keyword to create a copy of the object and then you are off to the races! Let’s see an example.
In this example, we’ll code the Class Ball and create a ball Color property and a bounce method. Follow along if you can. Initialize is a useful function that is run automatically when the object of the class is created. Although initialize can be left out, it is a good idea to add it in, even if it does nothing more than set the object up. You can use initialize to set up the default properties of the object. The left side is the creation of the Class, the right shows some code we can use to work with the class…we start by creating an instance of the Class using new and then we can do anything that is defined in the class – set the color, get the color, and bounce the ball. Note that when we ask the ball to bounce we tell it the number of times we want it to bounce and it does so. In the next section, I call a random function to set the color of the ball and then I print out the color of that particular ball. If I decided to create a second and third ball, they might be different colors – but each one would keep that property unless we change it.
So for this course, we will not really need to get you developing Classes to any great extent. Our purpose was really to get you familiar with the notion of object-oriented programming and development and to understand a little about how classes work and how you might create and use a class. I don’t suggest that you should create and use Classes in your project or assignment, but you are free to do so if you think you want to. OOP is a subject you would undertake in some detail at the next level or programming such as in our PROG 1040 Programming I or many of our courses in Programming and Web Concentrations. In our next session we are going to look at a technique to evaluate cause and effect in business or workplace situations using a tool called a Fishbone Diagram