text stringlengths 0 897 |
|---|
A> **Chapter Objectives** |
A> - Learn the basics of object-oriented programming. |
A> - Start thinking about how to make code that can be reused in different programs. |
By this point you've learned about all the important data types in MiniScript: numbers, strings, lists, maps, and functions. That's it; there are no more data types to learn! OK, technically `null` is its own data type, but it's a trivial one, and you already know about that too. |
So the rest of the book, including this chapter, is not going to teach you any more data types. But we are going to look at a particular use of the map data type. Maps are incredibly versatile, as we saw in Chapter 10. By mapping keys to values, they can represent pretty much anything. Now we're going to look at ho... |
## Classes and Objects |
{i:"OOP;object-oriented programming"} |
The terms *class* and *object* come from a long programming tradition called *object-oriented programming* (or OOP for short). Let's define what they mean in MiniScript terms: |
class |
: a map that is used to represent a type or category of thing, such as a Person, a Sprite, a Projectile, etc. |
object |
: a map that is used to represent a particular thing; examples might be Bob (a Person), space invader #7 (a Sprite), a softball at a certain position (a Projectile), etc. |
Notice that both classes and objects are just maps, and indeed, there is very little technical difference between them as far as MiniScript is concerned. The difference is in the intent of the programmer. They are used in different ways: classes hold code and data that is common to lots of different things. Objects ... |
All this is very abstract, so let's illustrate with an example. Suppose you're writing a program to work with colored blocks. And to make things simpler, imagine they're two-dimensional blocks, with only width and height and no depth — like what you might draw on a computer screen or graph paper. Fire up your REPL (... |
```miniscript |
Block = {} |
Block.color = "white" |
Block.width = 1 |
Block.height = 1 |
``` |
Line 1 here creates an empty map, and assigns it to `Block`. Lines 2-4 stuff some values into that map, using dot syntax. (Turn back to Chapter 10 if you need to review maps and dot syntax.) |
D> A common convention in the MiniScript world is to capitalize variable names that refer to a class. We intend to use `Block` here as a class, so we write it with a capital "B". |
{i: "`new`"} |
Now let's create an instance of the `Block` class. You do this using the `new` operator. Continuing from the previous example: |
```miniscript |
b1 = new Block |
print b1.width |
``` |
The first line also creates a map, but not just an empty map; it creates a map based on our previous `Block` map. We say that `b1` "is a" `Block`, or you might say that `b1` "inherits from" `Block`. These are all just ways of saying that `b1` was created from `Block` (or from some other map that itself *is a* `Block`... |
And that makes `b1` special, because it means that any time we look up a value in the `b1` map, if we don't find it there, we look at the map it inherits from. That's how we are able to print `b1.width` above, even though we never assigned a "width" value to `b1`. It's no problem; since `b1` doesn't have such a value... |
D> Under the hood, what `new` does isn't all that complex: it creates a map, but sets the special key `__isa` to whatever value comes after `new`. So in the example above, if you examine `b1`, you will find that `b1.__isa` is equal to `Block`. This is how MiniScript keeps track of what a map was made from, and does a... |
EBOOK>Here's a diagram of how `Block` and `b1` are represented in memory. Both are really just maps, but that special `__isa` entry in `b1` makes it an *instance* of class `Block`. |
{width:50%} |
 |
{i:"instance"} |
PRINT>A diagram of how `Block` and `b1` are represented in memory is at the top of the next page. Both are really just maps, but that special `__isa` entry in `b1` makes it an *instance* of class `Block`. |
instance |
: another word for "object," used when you want to emphasize the object's class |
At this point you can get a glimmer of what makes OOP (object-oriented programming) so useful. You may need a *lot* of blocks, and they probably have a lot of data and functions in common. Instead of putting redundant copies of all those bits of data and code on every block, you can put it on their common base class ... |
Let's continue with the above example by giving Block a function or two. Add this to your growing code: |
```miniscript |
Block.area = function() |
return self.width * self.height |
end function |
Block.description = function() |
return self.width + " x " + self.height + " " + self.color + " block" |
end function |
print b1.area |
print b1.description |
``` |
{i: "`self`"} |
Here we added two functions to the `Block` class, `area` and `description`. Each of these shows another OOP trick: the `self` keyword. `self` is a special reference to the map a function was called on with dot syntax. So if we call `b1.area`, then inside that `area` function, `self` refers to `b1`. The example abov... |
So far we have only one object of type `Block`, which is a little pointless. OOP becomes useful when there are many (or at least two) objects of the same kind. So, let's create a second block and call it `b2`. While we're at it, let's customize each of these blocks a bit so they're not boring 1-by-1 white squares. |
```miniscript |
b1.width = 3 |
b1.color = "red" |
b2 = new Block |
b2.height = 5 |
b2.color = "green" |
print b1.description |
print b2.description |
``` |
{width:"50%", position:"bottom"} |
 |
Now we're getting somewhere! We changed our `b1` block so that it is red and has a width of 3. We didn't change its height, so that's still 1. Then we created another block, `b2`, with a height of 5 and a green color. In this case we didn't change its width, so it gets 1 from `Block`. Neither of them has a custom ... |
That's the basics of OOP (object-oriented programming)! |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.