Lecture Date: Wednesday, March 18
Now that we have seen the basics of methods, we’re now going to put them together with the rest of the pieces of the puzzle to put together our own classes.
We’ll start by discussing the concept of what object orientation really means.
With an object-oriented language, we design our programs by picturing what are representable things in the world we are trying to model. Reasonable people can (and often do) disagree as to what exactly the good objects in a world might be, but for most of the examples we’ll look at it will be much simpler.
In general, when you are looking at the world you are trying to model, you want to identify the nouns and the verbs.
- Nouns become the classes and fields
- Verbs become the methods
The trick is in knowing what should be a class and what should be a field. A field is a class-level variable in a class and provides some information about that class.
For example, if
Student was a class, then
birthday would all be fields of the class.
When deciding if something should be a class or a field, consider that classes contain information about them and fields are the information for classes. So, if something has several facets to it, it’s probably a class. If something is simply a piece of data, it’s probably a field for some other class.
We’ll build a couple example classes as we figure all this out.