![]() ![]() Class names are traditionally capitalized (to distinguish them from variable names, which traditionally are lowercase).ĭata: The data for a class is a collection of variables. We then enclose all of the code for the class inside curly brackets after the name declaration. Here is how we can take the elements from a simple non-object-oriented sketch and place them into a Car class, from which we will then be able to make Car objects.Ĭlass Name: The name is specified by “class WhateverNameYouChoose”. (Technically, the only actual required element is the class name, but the point of doing object-oriented programming is to include all of these.) We will do exactly this with the car example, recreating exactly the same look and behavior in an object-oriented manner.Īll classes must include four elements: name, data, constructor, and methods. When you are first learning about object-oriented programming, it is often a useful exercise to take a program written without objects and, not changing the functionality at all, rewrite it using objects. The hard work goes into writing the object template-that is, the class itself. The simple Car example above demonstrates how the use of objects in Processing makes for clean, readable code. ![]() We are going to get into the details regarding the above code in a moment, but before we do so, let's take a look at how the Car class itself is written. Moving beyond pseudocode, the actual body of the sketch might look like: Car m圜ar Where did those variables go? They still exist, only now they live inside of the Car object (and will be defined in the Car class, which we will get to in a moment). Instead of having separate variables for car color, car location, and car speed, we now have only one variable: a Car variable! And, instead of initializing those three variables, we initialize one thing: the Car object. Notice: we removed all of the global variables from the first example. Using object-oriented design, the pseudocode improves to look something like this: The object will also know about the stuff it can do, the methods (functions inside an object)-the car can drive and it can be displayed. A car object will know about its data- color, location, speed. Object-oriented programming allows us to take all of the variables and functions out of the main program and store them inside a car object. To implement the above pseudo-code, we would define global variables at the top of the program, initialize them in setup(), and call functions to move and display the car in draw().
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |